Try/Multi-Catch vs Single Catch

后端 未结 5 955
北荒
北荒 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 18:02

    I believe that your first code snippet will only be applicable if your JDK is Java JDK 7. while the second snippet will still run in JDK 7 and below.

    If you have a line of code that is possible to throw different types of exception and you want to handle them all in a similar way, Multicatch will fit you since it saves several lines of code.

    however, if you have a line of code that will throw several exceptions, and you want to handle those exceptions individually, a single catch exception will fit you more.

提交回复
热议问题