Multiple try-catch or one?

前端 未结 11 1730

Normally, I\'d do this:

try
{
    code

    code that might throw an anticipated exception you want to handle

    code

    code that might throw an anticip         


        
11条回答
  •  Happy的楠姐
    2020-12-08 09:35

    Second method is better in my opinion because it allows you to trap errors with more accuracy.

    Also wrapping your entire code inside one big try/catch block is bad, if your app has some sort of problem and it crashes but since you trapped a big generic execption the chance that you can actualy handle it correctly is lower. You should have spesfic parts inside try catch, like if your reading a file or taking user input. That way you can better handle that exeception

提交回复
热议问题