Does code in finally get run after a return in Objective-C?

前端 未结 5 1323
广开言路
广开言路 2020-12-20 12:14

Consider the following code:

@try {
  if (something.notvalid)
  {
    return;
  }
  // do something else
} @catch (NSException *ex) {
  // handle exception
}         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 12:39

    Yes. Oddly enough, it does. I'm not sure why, but I just built a test and tried a number of configurations and every time it did.

    Here were the configs:

    • Return in try block: stopped execution of try block and caused finally to be executed
    • Return in try block and return in finally: stopped execution of try and stopped execution in finally block and the entire method.
    • Return in finally block: functioned like normal return outside of a try/catch/finally block.

提交回复
热议问题