How many statements in a try/catch statement?

前端 未结 13 1255
予麋鹿
予麋鹿 2021-01-12 12:42

Should I put multiple statements in a try and then catch all possible exceptions, or should I put only one statement in the try statement?

Example:



        
13条回答
  •  悲哀的现实
    2021-01-12 13:14

    If they are truly separate like that, then the first is better practice, simply because it's shorter.

    However, if it's possible for MaybeThrowIOException() to throw a FooBarException, or for MaybeThrowFooBarException() to throw a IOException, then you should only wrap them together if you want them both to take the same action upon an exception!

提交回复
热议问题