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