Is there a way to decorate a method that will do some logging, then throw an exception unconditionally, as such?
I have code like this:
void foo(out int x)
{
if( condition() ) { x = bar(); return; }
// notice that x is not yet set here, but compiler doesn't complain
throw new Exception( "missed something." );
}
If I try writing it like this I get a problem:
void foo(out int x)
{
if( condition() ) { x = bar(); return; }
// compiler complains about x not being set yet
MyMethodThatAlwaysThrowsAnException( "missed something." );
}
Any suggestions? Thanks.
How about this?
bool condition() { return false; }
int bar() { return 999; }
void foo(out int x)
{
if (condition()) { x = bar(); return; }
// compiler complains about x not being set yet
throw MyMethodThatAlwaysThrowsAnException("missed something.");
}
Exception MyMethodThatAlwaysThrowsAnException(string message)
{
//this could also be a throw if you really want
// but if you throw here the stack trace will point here
return new Exception(message);
}