开发者

Continue on exception

I get a few exceptions when I run my code. What I want to do is, I want to continue on FileNotFoundException and NullPointerException and break on any 开发者_开发知识库other exception.

How can I go about it? Thanks


try {
    stuff()
} catch( NullPointerException e ) {
   // Do nothing... go on
} catch( FileNotFoundException e ) {
   // Do nothing... go on
} catch( Exception e ) {
   // Now.. handle it!
}


You can do this as @daniel suggested, but I have some additional thoughts.

  1. You never want to 'do nothing'. At least log the fact that there was an exception.
  2. catching NullPointerExceptions can be dangerous. They can come from anywhere, not just the code where you expect the exception from. If you catch and proceed, you might get unintended results if you don't tightly control the code between the try/catch block.


multiple catch block to caught exception arised in try block

<code>
try{<br/>
// Code that may exception arise.<br/>
}catch(<exception-class1> <parameter1>){<br/>
//User code<br/>
}catch(<exception-class2> <parameter2>){<br/>
//User code<br/>
}catch(<exception-class3> <parameter3>){<br/>
//User code<br/>
}
</code>

Source : Tutorial Data - Exception handling

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜