捕获更一般的异常类型是否很好? [英] Is it good to catch a more general type of Exception?
问题描述
如果我们要捕获特定的 IOException
或者其他类型的
事实事实的形式,我们只尝试抓住一对(和为他们定义明确的输出)说
FileNotFoundException
ZipException我们应该总是追踪它,并覆盖所有的基地,一个
catch(IOException e){
e.printStackTrace();
}
然后可能走得更远,抓住异常e
,还是这个
完全浪费时间?
一般来说,想捕捉和处理例外,你可以在低级别做某事。然后在更高级别,捕获任何未处理的异常系统范围,所以您可以记录发生的错误。
If we are to catch specific forms of IOException
, or any other kind as a
matter of fact, and we only try and catch a couple (and define definitive outputs for them) say
FileNotFoundException
ZipException
should we always trail it off and cover all bases with a
catch(IOException e){
e.printStackTrace();
}
and then possibly go even further and catch Exception e
, or is this a
complete waste of time?
Generally, you only want to catch and handle exceptions you can do something with at a low level. Then at a higher level, catch any unhandled exceptions system wide, so you can record errors that occurred.
这篇关于捕获更一般的异常类型是否很好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!