How many statements in a try/catch statement?

前端 未结 13 1371
予麋鹿
予麋鹿 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:31

    The more statements you put, the less specific about the cause of exception you can be potentially.

    But of course it depends if the function calls/statements carry overlapped exceptions i.e. if all exceptions can be accounted for in a specific manner, then it is still ok.

    In your example, you seem to be having non-overlapped exceptions so your first form is ok.

提交回复
热议问题