4

Consider the following code:

try{
    ....

} catch(Exception) {
    return null;
    throw;
}

Does it make any sense?

Normally, if you put any codes after return, Visual Studio marks that as unreachable code, but for this case, it does not. Is there a reason for that?

Edit

As a complement to the question, I want to ask another one.

What happens if I throw the exception in the finally block, after return?

try{
    ....

} catch(Exception) {
    return null;
} finally {
    throw new Exception();
}
Ahmad
  • 5,551
  • 8
  • 41
  • 57
  • no after return rest of the part of the catch block won't reachable, return means you are returning something to its caller and after return call rest of the code have no value – Mostafiz Aug 05 '16 at 12:00
  • The `throw` is unreachable. The compiler recognizes it. https://msdn.microsoft.com/en-us/library/c0h4st1x.aspx – Tim Schmelter Aug 05 '16 at 12:01
  • Everything after a return statement would be unreachable, even if you had stuff in a finally block. – Drew Kennedy Aug 05 '16 at 12:06
  • 1
    @DrewKennedy: what does that mean? Of course a finally would be executed before the method returns `null` to the caller. It is executed always. – Tim Schmelter Aug 05 '16 at 12:08
  • @DrewKennedy see this: http://stackoverflow.com/q/345091/2040375 – Majid Aug 05 '16 at 12:09
  • @MajidRahimi Hmmm. Was pretty sure I saw somewhere the `finally` block would execute, but changes were essentially discarded. An example would be the catch block returning `null` (and is executed). After which it goes to the `finally` block and returns something else, it would still return `null` from the catch block. But I've been wrong before. :) – Drew Kennedy Aug 05 '16 at 12:11
  • 1
    You cannot return from a finally. That wouldn't compile – Tim Schmelter Aug 05 '16 at 12:26
  • "What happens if I throw the exception in the finally block, after return?": Why not try it out? – MakePeaceGreatAgain Aug 05 '16 at 12:26
  • @TimSchmelter As I see, compiler has no problem with that – Ahmad Aug 05 '16 at 12:27

3 Answers3

4

The throw after return is never executed. The exception is not thrown to the caller. It is an unreachable code.

It doesn't make any sense, a method that throws an exception doesn't return anything to the caller (except the exception itself) . So just throw.

try
{
    // return something valid
} 
catch(Exception) 
{
    throw;
}

As for why it is not marked by Visual Studio as unreachable, I think it is a bug.

Edit:

If you throw an exception in the finally block, the exception bubbles up the stack to find a catch and the value is not returned.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • Or just `return null` which actually is what the method currently does – Tim Schmelter Aug 05 '16 at 12:02
  • @TimSchmelter after the throw ? It is an unreachable code !. Before it, then the throw is unreachable. – Zein Makki Aug 05 '16 at 12:03
  • No, remove the `throw`, then there is no unreachable code and the method behaves as expected(returns `null` if an exception occured). Currently the `throw` does nothing, but the compiler recognizes that and shows a warning – Tim Schmelter Aug 05 '16 at 12:04
  • 1
    @TimSchmelter As stated in the OP's question, the compiler doesn't show any warning or unreachable code if throw is placed after the return inside a catch. – Zein Makki Aug 05 '16 at 12:08
  • Hmm, it is shown but not as a real warning but `throw` is greyed out and if you hover it you'll see that it's unreachable. But true, different behaviour if you use other code like `Console.Write("");` – Tim Schmelter Aug 05 '16 at 12:13
  • @TimSchmelter nothing grayed out in Visual Studio 2015 Community. – Zein Makki Aug 05 '16 at 12:14
  • maybe it is reflector. Look yourself: http://fs5.directupload.net/images/160805/j3as5wve.jpg – Tim Schmelter Aug 05 '16 at 12:18
3

It makes no sense at all to put any code after a return-statement. It can´t be executed at all, be it a valid statement or like in your case an exception. With return you go up the execution-stack and do not come back, so there is no way for the application to examine the code after that single return statement.

You should either return a specific value or throw an exception, doing both won´t work. Having said this both a return as well as a throw statement will leave the current method and go back to the calling-method.

As an aside there´s also the yield return. This works a bit different, as it means "return the next element from the iterator-method".

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
1

return and throw both will exit the method. Your catch block will never throw an exception because you can exit only once. So, throw is unreachable here. But the situation can be different if there is a finally block. Consider the following code.

try{
    ....

} 
catch(Exception) {
    return null;
    throw; // unreachable
}
finally
{
    //....
}

Codes inside the finally block will execute even if there are any exception and return null the return value will remain cached until finally executes.

EDIT

If finally block throws exception it'll propagate up and should be handled at a higher level. If the exception is not handled, the program will crash

For more you can follow this link

PaulShovan
  • 2,140
  • 1
  • 13
  • 22