智能摘要文章源自JAVA秀-https://www.javaxiu.com/37892.html
不仅仅初学者很难理解,即使一些有经验的开发者也需要花费很多时间来思考如何处理异常,包括需要处理哪些异常,怎样处理等等。尽可能的使用最具体的异常来声明方法,这样才能使得代码更容易理解。可以发现很多代码甚至类库中都会有捕获异常、记录日志并再次抛出的逻辑。这样可以添加更为具体的异常信息并能够做针对的异常处理。丢失了原始的异常信息会让错误的分析变得困难。文章源自JAVA秀-https://www.javaxiu.com/37892.html
原文约 2525 字 | 图片 8 张 | 建议阅读 6 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/37892.html
处理 Exception 的几种实践,很优雅,已被很多团队采纳!
点击关注 ? Java面试那些事儿 文章源自JAVA秀-https://www.javaxiu.com/37892.html
收录于话题文章源自JAVA秀-https://www.javaxiu.com/37892.html
#Java面试那些事儿文章源自JAVA秀-https://www.javaxiu.com/37892.html
224个文章源自JAVA秀-https://www.javaxiu.com/37892.html
大家好,我是D哥点击关注下方公众号,Java面试资料 都在这里
文章源自JAVA秀-https://www.javaxiu.com/37892.html
来源:http://ww7.rowkey.me/文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.html
在Java中处理异常并不是一个简单的事情。不仅仅初学者很难理解,即使一些有经验的开发者也需要花费很多时间来思考如何处理异常,包括需要处理哪些异常,怎样处理等等。文章源自JAVA秀-https://www.javaxiu.com/37892.html
这也是绝大多数开发团队都会制定一些规则来规范对异常的处理的原因。而团队之间的这些规范往往是截然不同的。文章源自JAVA秀-https://www.javaxiu.com/37892.html
本文给出几个被很多团队使用的异常处理最佳实践。文章源自JAVA秀-https://www.javaxiu.com/37892.html
# 在Finally块中清理资源或者使用try-with-resource语句
文章源自JAVA秀-https://www.javaxiu.com/37892.html当使用类似InputStream这种需要使用后关闭的资源时,一个常见的错误就是在try块的最后关闭资源。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.htmlpublicvoiddoNotCloseResourceInTry(){
FileInputStream inputStream = null;
try {
File file = new File("./tmp.txt");
inputStream = new FileInputStream(file);
// use the inputStream to read a file
// do NOT do this
inputStream.close();
} catch (FileNotFoundException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
}
}
上述代码在没有任何exception的时候运行是没有问题的。但是当try块中的语句抛出异常或者自己实现的代码抛出异常,那么就不会执行最后的关闭语句,从而资源也无法释放。文章源自JAVA秀-https://www.javaxiu.com/37892.html
合理的做法则是将所有清理的代码都放到finally块中或者使用try-with-resource语句。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.htmlpublicvoidcloseResourceInFinally(){
FileInputStream inputStream = null;
try {
File file = new File("./tmp.txt");
inputStream = new FileInputStream(file);
// use the inputStream to read a file
} catch (FileNotFoundException e) {
log.error(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error(e);
}
}
}
}
publicvoidautomaticallyCloseResource(){
File file = new File("./tmp.txt");
try (FileInputStream inputStream = new FileInputStream(file);) {
// use the inputStream to read a file
} catch (FileNotFoundException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
}
}
# 指定具体的异常文章源自JAVA秀-https://www.javaxiu.com/37892.html
尽可能的使用最具体的异常来声明方法,这样才能使得代码更容易理解。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.htmlpublicvoiddoNotDoThis()throws Exception {
...
}
publicvoiddoThis()throws NumberFormatException {
...
}
如上,NumberFormatException字面上即可以看出是数字格式化错误。文章源自JAVA秀-https://www.javaxiu.com/37892.html
# 对异常进行文档说明
文章源自JAVA秀-https://www.javaxiu.com/37892.html当在方法上声明抛出异常时,也需要进行文档说明。和前面的一点一样,都是为了给调用者提供尽可能多的信息,从而可以更好地避免/处理异常。异常处理的 10 个最佳实践,这篇也推荐看下。文章源自JAVA秀-https://www.javaxiu.com/37892.html
在Javadoc中加入throws声明,并且描述抛出异常的场景。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.html/**
* This method does something extremely useful ...
*
* @param input
* @throws MyBusinessException if ... happens
*/
publicvoiddoSomething(String input)throws MyBusinessException {
...
}
文章源自JAVA秀-https://www.javaxiu.com/37892.html
# 抛出异常的时候包含描述信息文章源自JAVA秀-https://www.javaxiu.com/37892.html
在抛出异常时,需要尽可能精确地描述问题和相关信息,这样无论是打印到日志中还是监控工具中,都能够更容易被人阅读,从而可以更好地定位具体错误信息、错误的严重程度等。文章源自JAVA秀-https://www.javaxiu.com/37892.html
但这里并不是说要对错误信息长篇大论,因为本来Exception的类名就能够反映错误的原因,因此只需要用一到两句话描述即可。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.htmltry {
new Long("xyz");
} catch (NumberFormatException e) {
log.error(e);
}
NumberFormatException即告诉了这个异常是格式化错误,异常的额外信息只需要提供这个错误字符串即可。当异常的名称不够明显的时候,则需要提供尽可能具体的错误信息。文章源自JAVA秀-https://www.javaxiu.com/37892.html
# 首先捕获最具体的异常
文章源自JAVA秀-https://www.javaxiu.com/37892.html现在很多IDE都能智能提示这个最佳实践,当你试图首先捕获最笼统的异常时,会提示不能达到的代码。当有多个catch块中,按照捕获顺序只有第一个匹配到的catch块才能执行。因此,如果先捕获IllegalArgumentException,那么则无法运行到对NumberFormatException的捕获。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.htmlpublicvoidcatchMostSpecificExceptionFirst(){
try {
doSomething("A message");
} catch (NumberFormatException e) {
log.error(e);
} catch (IllegalArgumentException e) {
log.error(e)
}
}
# 不要捕获Throwable文章源自JAVA秀-https://www.javaxiu.com/37892.html
Throwable是所有异常和错误的父类。你可以在catch语句中捕获,但是永远不要这么做。如果catch了throwable,那么不仅仅会捕获所有exception,还会捕获error。而error是表明无法恢复的jvm错误。因此除非绝对肯定能够处理或者被要求处理error,不要捕获throwable。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.htmlpublicvoiddoNotCatchThrowable() {
try {
// do something
} catch (Throwable t) {
// don't do this!
}
}
# 不要忽略异常文章源自JAVA秀-https://www.javaxiu.com/37892.html
很多时候,开发者很有自信不会抛出异常,因此写了一个catch块,但是没有做任何处理或者记录日志。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.htmlpublicvoiddoNotIgnoreExceptions() {
try {
// do something
} catch (NumberFormatException e) {
// this will never happen
}
}
文章源自JAVA秀-https://www.javaxiu.com/37892.html
但现实是经常会出现无法预料的异常或者无法确定这里的代码未来是不是会改动(删除了阻止异常抛出的代码),而此时由于异常被捕获,使得无法拿到足够的错误信息来定位问题。合理的做法是至少要记录异常的信息。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.htmlpublicvoidlogAnException(){
try {
// do something
} catch (NumberFormatException e) {
log.error("This should never happen: " + e);
}
}
# 不要记录并抛出异常文章源自JAVA秀-https://www.javaxiu.com/37892.html
可以发现很多代码甚至类库中都会有捕获异常、记录日志并再次抛出的逻辑。如下:文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.htmltry {
new Long("xyz");
} catch (NumberFormatException e) {
log.error(e);
throw e;
}
这个处理逻辑看着是合理的。但这经常会给同一个异常输出多条日志。如下:文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.html17:44:28,945 ERRORTestExceptionHandling:65-java.lang.NumberFormatException: Forinputstring: "xyz"
Exceptioninthread "main" java.lang.NumberFormatException: Forinputstring: "xyz"
atjava.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
atjava.lang.Long.parseLong(Long.java:589)
atjava.lang.Long.(Long.java:965)
atcom.stackify.example.TestExceptionHandling.logAndThrowException(TestExceptionHandling.java:63)
atcom.stackify.example.TestExceptionHandling.main(TestExceptionHandling.java:58)
如上所示,后面的日志也没有附加更有用的信息。如果想要提供更加有用的信息,那么可以将异常包装为自定义异常。
文章源自JAVA秀-https://www.javaxiu.com/37892.htmlpublicvoidwrapException(String input)throws MyBusinessException {
try {
// do something
} catch (NumberFormatException e) {
thrownew MyBusinessException("A message that describes the error.", e);
}
}
文章源自JAVA秀-https://www.javaxiu.com/37892.html
因此,仅仅当想要处理异常时才去捕获,否则只需要在方法签名中声明让调用者去处理文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.html
# 包装异常时不要抛弃原始的异常
文章源自JAVA秀-https://www.javaxiu.com/37892.html捕获标准异常并包装为自定义异常是一个很常见的做法。这样可以添加更为具体的异常信息并能够做针对的异常处理。文章源自JAVA秀-https://www.javaxiu.com/37892.html
需要注意的是,包装异常时,一定要把原始的异常设置为cause(Exception有构造方法可以传入cause)。否则,丢失了原始的异常信息会让错误的分析变得困难。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.htmlpublicvoidwrapException(String input)throws MyBusinessException {
try {
// do something
} catch (NumberFormatException e) {
thrownew MyBusinessException("A message that describes the error.", e);
}
}
# 总结
文章源自JAVA秀-https://www.javaxiu.com/37892.html综上可知,当抛出或者捕获异常时,有很多不一样的东西需要考虑。其中的许多点都是为了提升代码的可阅读性或者api的可用性。文章源自JAVA秀-https://www.javaxiu.com/37892.html
异常不仅仅是一个错误控制机制,也是一个沟通媒介,因此与你的协作者讨论这些最佳实践并制定一些规范能够让每个人都理解相关的通用概念并且能够按照同样的方式使用它们。文章源自JAVA秀-https://www.javaxiu.com/37892.html
文章源自JAVA秀-https://www.javaxiu.com/37892.html
技术交流群有不少同学问D哥,大厂面试官到底喜欢问什么?想进大厂镀金。因此,D哥特意邀请了华为、腾讯、阿里的朋友进群,与大家一起交流经验,增长技术。有兴趣入群的同学,可长按扫描下方二维码,一定要备注:城市+昵称+技术方向,根据格式备注,可更快被通过且邀请进群。▲长按扫描
热门推荐文章源自JAVA秀-https://www.javaxiu.com/37892.html
这些IDEA的优化设置赶紧安排起来,效率提升杠杠的!if(a==1 && a==2 && a==3),有没有可能为true???主流App开屏广告一夜消失!文章源自JAVA秀-https://www.javaxiu.com/37892.html

评论