473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

custom Throw() function

Hi,
I have function which is throwning exception, like this:

public class LoggedException : Exception
{
public void Throw()
{
throw this;
}
}

I am using like this:

public int fun()
{
if (..)
return 0;
(new LoggedException()).Throw(); <-- Compiler Error CS0161
}

But I get 'Compiler Error CS0161: not all code paths return a value'.
How can I tell to compiler that I don't need return value when I call
Throw() function?
Thank you,
Nenad
Nov 16 '05 #1
3 2343
Nenad,

You can not. The compiler doesn't know that the Throw method will
always throw an exception, so you need to place a return statement after the
call to Throw.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nenad Dobrilovic" <ch*******@hotmail.com> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
Hi,
I have function which is throwning exception, like this:

public class LoggedException : Exception
{
public void Throw()
{
throw this;
}
}

I am using like this:

public int fun()
{
if (..)
return 0;
(new LoggedException()).Throw(); <-- Compiler Error CS0161
}

But I get 'Compiler Error CS0161: not all code paths return a value'.
How can I tell to compiler that I don't need return value when I call
Throw() function?
Thank you,
Nenad

Nov 16 '05 #2
Nicholas Paldino [.NET/C# MVP] wrote:
Nenad,

You can not. The compiler doesn't know that the Throw method will
always throw an exception, so you need to place a return statement after the
call to Throw.

Hope this helps.


Thank you for your quick answer.
I was hoping that I can tell him, beacuse he is not clever enough to
find out by himself.
Nov 16 '05 #3
Nenad Dobrilovic wrote:
Hi,
I have function which is throwning exception, like this:

public class LoggedException : Exception
{
public void Throw()
{
throw this;
}
}

I am using like this:

public int fun()
{
if (..)
return 0;
(new LoggedException()).Throw(); <-- Compiler Error CS0161
}

But I get 'Compiler Error CS0161: not all code paths return a value'.
How can I tell to compiler that I don't need return value when I call
Throw() function?


You have several options:

//Option 1:
public int fun()
{
if (!(..)) {
(new LoggedException()).Throw();
}

// ...
return 0;
}
// Option 2:
public class LoggedException : Exception
{
public LoggedException GetException()
{
return( this);
}
}
public int fun()
{
if (..)
return 0;
throw (new LoggedException()).GetException();
}

//Option 3:
// put whatever logging functionality you would have put
// into Throw() (or GetException()) into the
// LoggedException() constructor and simply do:

throw new LoggedException();

--
mikeb
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nick | last post by:
My client uses a SQL Database to store their usernames and passwords, and I do not believe they have AD...no big deal... I wrote a class to create a generic identity and generic principal so that...
1
by: Craig | last post by:
I have added a 'Textboxes (A)' to my UI installer project along with a custom action to pass the value back to a class I've written to override the void Install function. As long as the text is...
8
by: Tinus | last post by:
Hello all, I've create a custom control (UserControl) and have a custom Item Collection. The control is a custom calendar which is draw using the Graphics Rectangle etc. functions. It is drawn...
2
by: S. Justin Gengo | last post by:
Hi, I've created a component that allows me to store database information for various types of databases my company uses. It uses a collection for each type of database. Everything is working...
0
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to...
3
by: DC Gringo | last post by:
Hi, I'm trying to use a custom action to modify a database (rather than create one) using the VS.NET '03's help example called "Custom Action to Create Database During Installation". I've made...
6
by: Steve Amey | last post by:
Hi all I want to be able to throw a custom error up the call stack. I have looked around and it seems as though it's possible, but I can't get it to work :o( Below is some sample code. ...
1
by: Jakob Lithner | last post by:
When I started a new ASP project I was eager to use the login facilities offered in Framework 2.0/VS 2005. I wanted: - A custom principal that could hold my integer UserID from the database -...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
3
by: =?Utf-8?B?Vk1J?= | last post by:
How can I catch an error and throw the exception message plus some custom text? For example, I'd like to do something like this: try { //generate error } catch { //throw ex + " my custom...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.