捕获更一般的异常类型是否很好? [英] Is it good to catch a more general type of Exception?

查看:117
本文介绍了捕获更一般的异常类型是否很好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们要捕获特定的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆