views:

341

answers:

6

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.

+2  A: 

If you know the exception will always be thrown, why does it matter. Just set the variable to something so it can compile:

void foo(out int x)
{
  if( condition() ) { x = bar(); return; }

  x = 0;

  MyMethodThatAlwaysThrowsAnException( "missed something." );
}
Dan Herbert
That's exactly the thing I wanted to avoid doing. :)
k0dek0mmand0
+1  A: 

It doesn't answer your question but when using out parameters it is always a good idea to initialize them in the beginning of the method. This way you won't have any compiler errors:

void foo(out int x)
{
    x = 0;
    if( condition() ) { x = bar(); return; }
    MyMethodThatAlwaysThrowsAnException( "missed something." );

}

Darin Dimitrov
I think out parameters should be assigned where it makes sense or as it may prevent some hidden bugs at compile time just because you forgot to assign out parameter at some code path.
Dzmitry Huba
+1  A: 

x is an out parameter and must be set before you move forward

Chris Ballance
unless an exception is thrown
k0dek0mmand0
It is still good policy to ensure that any out parameters are set early on in the method and assign defaults to them if not.
Chris Ballance
+2  A: 

There's no way of marking a method in this way.

Possibly irrelevant, but the pattern in your example, using an out parameter, is a bit odd. Why not just have a return type on the method instead?

int Foo()
{
    if (condition()) return bar();

    MyMethodThatAlwaysThrowsAnException("missed something.");
}
LukeH
That was just a simple example. The actual code is rather more complex.
k0dek0mmand0
@k0dek0mmand0: I suspected that might be the case. I guess you're out of luck - there's no way to tell the compiler that `MyMethodThatAlwaysThrowsAnException` always throws.
LukeH
+9  A: 

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);
}
Matthew Whited
A: 

If you don't want to have to set x, why don't you just use a ref parameter instead?

void foo(ref int x)
{
  if( condition() ) { x = bar(); return; }

  // nobody complains about anything

  MyMethodThatAlwaysThrowsAnException( "missed something." );
}
Maximilian Mayerl