java提供了异常处理机制:程序运行受阻时候的处理方式。
1、异常分类
2、声明异常:在方法声明中使用throws声明异常
public void method() throws ClassNotFoundException{ //... }
注意:子类重写了父类的方法,子类的方法抛出的异常必须在被覆盖的父类的方法中声明中,否则不满足java的多态使用,会报错。
3、抛出异常:在方法内部使用throw抛出异常,在方法上必须先声明异常
package com.yichang; public class Main { public static void main(String[] args) { try { method1(); }catch (Exception e){ e.printStackTrace(); } } public static void method1() throws Exception{ try { method2(); }catch (Exception e) { throw e;
//throw new Exception("method1 exception", e);或者 throw new Exception(e);
//throw throw (Exception)e.fillInStackTrace();
} } public static void method2() throws Exception{ throw new Exception("method2 exception"); } }
java.lang.Exception: method2 exception at com.yichang.Main.method2(Main.java:24) at com.yichang.Main.method1(Main.java:17) at com.yichang.Main.main(Main.java:8)
java.lang.Exception: method1 exception at com.yichang.Main.method1(Main.java:19) at com.yichang.Main.main(Main.java:8) Caused by: java.lang.Exception: method2 exception at com.yichang.Main.method2(Main.java:24) at com.yichang.Main.method1(Main.java:17) ... 1 more
或者如上throw new Exception(e)
java.lang.Exception: java.lang.Exception: method2 exception at com.yichang.Main.method1(Main.java:19) at com.yichang.Main.main(Main.java:8) Caused by: java.lang.Exception: method2 exception at com.yichang.Main.method2(Main.java:24) at com.yichang.Main.method1(Main.java:17) ... 1 more
java.lang.Exception: method2 exception at com.yichang.Main.method1(Main.java:29) at com.yichang.Main.main(Main.java:8)
4、捕获异常:使用catch捕获处理异常
try { //业务逻辑 catch (IOException){ //异常处理 }catch (Exception e){ //异常处理 }finally { }
getMessage()
toString() 返回格式:“异常全类名:getMessage的字符串”
printStackTrace() 在控制台打印异常调用栈信息,getStackTrace()获取StackTraceElemen[]数组
main函数修改如下:
public static void main(String[] args) { try { method1(); }catch (Exception e){ e.printStackTrace(); System.out.println(e.getMessage()); System.out.println(e.toString()); StackTraceElement[] stackTraceElements=e.getStackTrace(); for (StackTraceElement stackTraceElement: stackTraceElements) { System.out.println(stackTraceElement.getMethodName()+" " +stackTraceElement.getClassName()+" " +stackTraceElement.getLineNumber()); } } }
输出结果:
java.lang.Exception: method1 exception at com.yichang.Main.method1(Main.java:27) at com.yichang.Main.main(Main.java:8) Caused by: java.lang.Exception: method2 exception at com.yichang.Main.method2(Main.java:32) at com.yichang.Main.method1(Main.java:25) ... 1 more method1 exception java.lang.Exception: method1 exception method1 com.yichang.Main 27 main com.yichang.Main 8
package com.yichang; public class Main { public static void main(String[] args) { int num=method1(); System.out.println("main方法中的num="+num); } public static int method1(){ int num=100; try { return ++num; }catch (Exception e) { e.printStackTrace(); }finally { num--; System.out.println("执行finally块, 当前num="+num); } return -1; } }
输出结果:
执行finally块, 当前num=100 main方法中的num=101
package com.yichang; public class Main { public static void main(String[] args) { int num=method1(); System.out.println("main方法中的num="+num); } public static int method1(){ try { throw new RuntimeException("异常了..."); }finally { System.out.println("执行finally"); return 1; } }
输出结果:
执行finally main方法中的num=1
5、自定义异常
通常是继承Exception类,并使用默认构造器,最简单的自定义异常为:
package com.yichang; public class MyException extends Exception { }
如对本文有疑问, 点击进行留言回复!!
关于在IDEA中SpringBoot项目中activiti工作流的使用详解
视频编辑工具ACDSee Luxea Video Editor安装及激活图文教程
java spring整合junit操作(有详细的分析过程)
网友评论