Try/Multi-Catch vs Single Catch

后端 未结 5 982
北荒
北荒 2021-01-11 17:16

While adding a try/catch block in Eclipse, it gave me the option of \"Surround with try/multi-catch\" or \"Surround with try/catch.\"

This is the try/multi-catch:

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-11 17:47

    This a choice thing. You want to balance readability, portability, maintainability and also handling different exceptions differently.

    So balance the use ... If all your catches use the same block of handling then use the first form, because that's just one code block and you aren't repeating yourself over and over again. The compiler can optimize things out a bit for you.

    On the other hand use the second form if you are handling each exception differently.

    This is somewhat of a broad question and the answer is dependant on your goals.

提交回复
热议问题