一、尽量不要使用e.printstacktrace(),而是使用log打印。
反例:
try{ // do what you want }catch(exception e){ e.printstacktrace();}
正例:
try{ // do what you want }catch(exception e){ log.info("你的程序有异常啦,{}",e);}
理由:
二、catch了异常,但是没有打印出具体的exception,无法更好定位问题
反例:
try{ // do what you want }catch(exception e){ log.info("你的程序有异常啦");}
正例:
try{ // do what you want }catch(exception e){ log.info("你的程序有异常啦,{}",e);}
理由:
三、不要用一个exception捕捉所有可能的异常
反例:
public void test(){ try{ //…抛出 ioexception 的代码调用 //…抛出 sqlexception 的代码调用 }catch(exception e){ //用基类 exception 捕捉的所有可能的异常,如果多个层次都这样捕捉,会丢失原始异常的有效信息哦 log.info(“exception in test,exception:{}”, e); }}
正例:
public void test(){ try{ //…抛出 ioexception 的代码调用 //…抛出 sqlexception 的代码调用 }catch(ioexception e){ //仅仅捕捉 ioexception log.info(“ioexception in test,exception:{}”, e); }catch(sqlexception e){ //仅仅捕捉 sqlexception log.info(“sqlexception in test,exception:{}”, e); }}
理由:
四、记得使用finally关闭流资源或者直接使用try-with-resource
反例:
fileinputstream fdin = null;try { fdin = new fileinputstream(new file("/jay.txt")); //在这里关闭流资源?有没有问题呢?如果发生异常了呢? fdin.close();} catch (filenotfoundexception e) { log.error(e);} catch (ioexception e) { log.error(e);}
正例1:
需要使用finally关闭流资源,如下
fileinputstream fdin = null;try { fdin = new fileinputstream(new file("/jay.txt"));} catch (filenotfoundexception e) { log.error(e);} catch (ioexception e) { log.error(e);}finally { try { if (fdin != null) { fdin.close(); } } catch (ioexception e) { log.error(e); }}
正例2:
当然,也可以使用jdk7的新特性try-with-resource来处理,它是java7提供的一个新功能,它用于自动资源管理。
try (fileinputstream inputstream = new fileinputstream(new file("jay.txt")) { // use resources } catch (filenotfoundexception e) { log.error(e);} catch (ioexception e) { log.error(e);}
理由:
五、捕获异常与抛出异常必须是完全匹配,或者捕获异常是抛异常的父类
反例:
//bizexception 是 exception 的子类public class bizexception extends exception {}//抛出父类exceptionpublic static void test() throws exception {} try { test(); //编译错误} catch (bizexception e) { //捕获异常子类是没法匹配的哦 log.error(e);}
正例:
//抛出子类exceptionpublic static void test() throws bizexception {} try { test();} catch (exception e) { log.error(e);}
六、捕获到的异常,不能忽略它,至少打点日志吧
反例:
public static void testignoreexception() throws exception { try { // 搞事情 } catch (exception e) { //一般不会有这个异常 }}
正例:
public static void testignoreexception() { try { // 搞事情 } catch (exception e) { //一般不会有这个异常 log.error("这个异常不应该在这里出现的,{}",e); }}
理由:
七、注意异常对你的代码层次结构的侵染(早发现早处理)
反例:
public userinfo queryuserinfobyuserid(long userid) throw sqlexception { //根据用户id查询数据库}
正例:
public userinfo queryuserinfobyuserid(long userid) { try{ //根据用户id查询数据库 }catch(sqlexception e){ log.error("查询数据库异常啦,{}",e); }finally{ //关闭连接,清理资源 }}
理由:
八、自定义封装异常,不要丢弃原始异常的信息throwable cause
我们常常会想要在捕获一个异常后抛出另一个异常,并且希望把原始异常的信息保存下来,这被称为异常链。公司的框架提供统一异常处理就用到异常链,我们自定义封装异常,不要丢弃原始异常的信息,否则排查问题就头疼啦
反例:
public class testchainexception { public void readfile() throws myexception{ try { inputstream is = new fileinputstream("jay.txt"); scanner in = new scanner(is); while (in.hasnext()) { system.out.println(in.next()); } } catch (filenotfoundexception e) { //e 保存异常信息 throw new myexception("文件在哪里呢"); } } public void invokereadfile() throws myexception{ try { readfile(); } catch (myexception e) { //e 保存异常信息 throw new myexception("文件找不到"); } } public static void main(string[] args) { testchainexception t = new testchainexception(); try { t.invokereadfile(); } catch (myexception e) { e.printstacktrace(); } }}//myexception 构造器public myexception(string message) { super(message); }
运行结果如下,没有了throwable cause,不好排查是什么异常了
正例:
public class testchainexception { public void readfile() throws myexception{ try { inputstream is = new fileinputstream("jay.txt"); scanner in = new scanner(is); while (in.hasnext()) { system.out.println(in.next()); } } catch (filenotfoundexception e) { //e 保存异常信息 throw new myexception("文件在哪里呢", e); } } public void invokereadfile() throws myexception{ try { readfile(); } catch (myexception e) { //e 保存异常信息 throw new myexception("文件找不到", e); } } public static void main(string[] args) { testchainexception t = new testchainexception(); try { t.invokereadfile(); } catch (myexception e) { e.printstacktrace(); } }}//myexception 构造器public myexception(string message, throwable cause) { super(message, cause); }
九、运行时异常runtimeexception ,不应该通过catch 的方式来处理,而是先预检查,比如:nullpointerexception处理
反例:
try { obj.method() } catch (nullpointerexception e) {...}
正例:
if (obj != null){ ...}
十、注意异常匹配的顺序,优先捕获具体的异常
注意异常的匹配顺序,因为只有第一个匹配到异常的catch块才会被执行。如果你希望看到,是numberformatexception异常,就抛出numberformatexception,如果是illegalargumentexception就抛出illegalargumentexception。
反例:
try { dosomething("test exception");} catch (illegalargumentexception e) { log.error(e);} catch (numberformatexception e) { log.error(e);}
正例:
try { dosomething("test exception");} catch (numberformatexception e) { log.error(e);} catch (illegalargumentexception e) { log.error(e);}
理由:
在这里说一下自己整理了一份关于java的系统化资料,从javase- ssm-springcloud,包括了面试题,pdf电子书,网上商城项目,个人博客项目,分布式项目等都有睁开想学习java或者转行,大学生都非常实用
java交流学习资源大全文件下载加我的交流学习群,里面有学习手册,面试题,开发工具,pdf文档书籍教程
到此这篇关于java 编程中十个处理异常的建议的文章就介绍到这了,更多相关java 异常处理内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!
如对本文有疑问, 点击进行留言回复!!
[杭电多校2020]第一场 1004 Distinct Sub-palindromes
Swift -- 将本地生成的UIImage进行持久化保存(存到文件中fileManager.createFile)
网友评论