2011-01-20 78 views
0

我运行代码时遇到了一些例外情况。 我想要做的是,我要继续FileNotFoundExceptionNullPointerException和任何其他异常中断。继续例外

我怎么能走呢? 感谢

+11

您应该修正你的代码,以消除这些例外。对于抛出`NullPointerException`的代码,没有**的借口。 – SLaks 2011-01-20 20:45:57

+1

抛出NullPointerExceptions的代码不太可能正常工作。 – 2011-01-20 20:47:49

回答

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

你可以这样做,因为@daniel建议,但我有一些额外的想法。

  1. 你永远要 '做什么'。至少记录有一个例外的事实。
  2. 捕捉NullPointerException异常可能是危险的。他们可以来自任何地方,而不仅仅是您希望例外的代码。如果你捕捉并继续,如果你不严格控制try/catch块之间的代码,你可能会得到意想不到的结果。
0

多个catch块捕获的异常在try块arised

<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> 

来源:Tutorial Data - Exception handling