Difference between try-finally and try-catch

前端 未结 11 549
栀梦
栀梦 2020-12-23 03:05

What\'s the difference between

try {
    fooBar();
} finally {
    barFoo();
}

and

try {
  fooBar();
} catch(Throwable thro         


        
11条回答
  •  一整个雨季
    2020-12-23 03:18

    Finally and catch blocks are quite different:

    Within the catch block you can respond to the thrown exception. This block is executed only if there is an unhandled exception and the type matches the one or is subclass of the one specified in the catch block's parameter. Finally will be always executed after try and catch blocks whether there is an exception raised or not.

提交回复
热议问题