C # - Rtrow exception in catch block, after return

Consider the following code:

try{
    ....

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

Is there any reason?

Usually, if you put the codes after the return, Visual Studio marks this as unreachable code, but for this case it is not. Is there a reason for this?

Edit

In addition to the question, I want to ask another question.

What happens if I make an exception in the finally block after returning?

try{
    ....

} catch(Exception) {
    return null;
} finally {
    throw new Exception();
}
+4
source share
3 answers

throwafter returnnever executed. An exception is not thrownfor the caller. This is an unreachable code.

, , , ( ). .

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

, Visual Studio , , .

:

finally, , , .

+4

- return -statement. , . return , return.

, . , a return, throw .

yield return. , " ".

+3

return throw . catch , . , throw . , finally. .

try{
    ....

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

finally , - , return null , , , .

If the final exception blocks, it will propagate and should be handled at a higher level. If the exception is not handled, the program will fail

For more information you can follow this link.

+1
source

Source: https://habr.com/ru/post/1650299/


All Articles