why must return statement precede a throw statement in a catch block

后端 未结 5 452
后悔当初
后悔当初 2020-12-20 12:28

The code below will complain

try
{
    session.Save(obj);
    return true;
}
catch (Exception e)
{
    throw e;
    return false;  // this will be flagged as         


        
5条回答
  •  太阳男子
    2020-12-20 12:44

    You are wrong: both your examples raise the Dead code compiler error because both throw and return mark the exit point of a method and no further code is allowed beyond that point.

    However, whether the compiler allows it or not, the code below either the throw or the return is still dead and will never get a chance to execute.

    (NOTE: this question was initially tagged as Java and my first sentence pertains to Java compiler semantics)

提交回复
热议问题