Error
StackOverflowError(栈溢出)
OutOfMemoryError(内存溢出)
非必检异常:
_NullPointerException(空指针)
_
ClassCastException(类型转换异常)
IndexOutOfBoundsException(越界异常)
ArithmeticException(算术异常)
IllegalArgumentException (非法参数异常)
必检异常:
ClassNotFoundException(未找到类)
NoSuchMethodException(未找到方法)
SocketException(socket异常)继承IOException
java提供了异常处理机制:程序运行受阻时候的处理方式。
1、异常分类
- Error:系统错误,由java虚拟机抛出,很少发生;免检异常
- RuntimeException:程序设计错误,通常由java虚拟机抛出;免检异常
- Exception的其它异常:必检异常,通过try-catch或者throws申明抛出
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");
}
}
-
可以抛出新建的异常,如上 throw new Exception("method2 exception");
-
重新抛出异常
-
重新抛出异常--抛出当前捕获的异常,如上method1的 throw e;
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)
-
重新抛出异常--抛出链式异常:捕获一个异常后抛出另一个异常,并把原始的异常信息保存下了,如上throw new Exception("method1 exception", e)
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
-
重新抛出异常--当前抛出点为异常新的抛出点,见throw throw (Exception)e.fillInStackTrace(); 其中fillInStackTrace()方法返回Throwable,需强制类型转换,以便被catch捕获
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 {
}
- 捕获到异常e,可以使用异常API获取详细的信息
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
finally 与 return 、break 、continue使用时,finally也会得到执行
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
-
异常丢失:如果在finally中有return,在try中抛出了异常,且没有捕获,会造成异常丢失
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 {
}