14. 异常机制
14.1 异常概念
异常是程序在运行期发生的不正常的事件,它会打断指令的正常执行流程。
设计良好的程序应该在异常发生时提供处理这些不正常事件的方法,使程序不会因为异常的发生而阻断或产生不可预见的结果。
Java语言使用异常处理机制为程序提供了异常处理的能力
14.2 异常分类
Java程序运行过程中所发生的异常事件从严重性可分为两类: 错误(Error)和异常(Exception)
14.2.1 错误-Error
JVM系统内部错误或资源耗尽等严重情况属于JVM需要承担的责任,这一类异常事件无法恢复或不可能被捕获,将导致应用程序中断。
常见的Error:
- StackOverflowError ----栈内存溢出的错误,例:
//无节制的递归 public static void main(String[] args) method(); } public static void method(){ method(); }
- OutOfMemoryError ---- 内存溢出的错误,例:
//定义超大的空间 ArrayList<String> list = new ArrayList<>(Integer.MAX_VALUE);
14.2.2 异常-Exception
其它因编程错误或偶然的外在因素导致的一般性问题。这类异常得到恰当的处理时,程序有机会恢复至正常运行状况。
根据编译器是否强制要求处理可分为:受检(checked)异常和非受检(unchecked)异常
14.2.2.1 受检(checked)异常–一般性异常
编译器要求**必须处置的异常,指的是程序在运行时由于外界因素造成的一般性异常。
常见的受检(checked)异常:
- ParseException ---- 解析异常
//需要解析的字符串格式错误 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse("2021-8-12- 10:56:01"); System.out.println(date);
- ClassNotFoundException ----类未找到异常
//获取Student类字节码文件对象 Class<?> c = Class.forName("com.dream.test.A");
- FileNotFoundException ----文件未找到异常
File file = new File("C:\\Users\\test.txt");
- IOException ----输入输出异常
FileInputStream fis = new FileInputStream(file);
14.2.2.2 非受检(unchecked)异常–运行时异常 RuntimeException
编译器不要求强制处置的异常。一般是指编程时的逻辑错误。是程序员应该积极避免其出现的异常
常见的非受检(unchecked)异常:
- ArithmeticException ----算数异常
//除数为0 System.out.println(10/0);
- ArrayIndexOutOfBoundsException ----数组下标越界异常
//访问的数组下标超出范围 int[] is = new int[10]; System.out.println(is[100]);
- ClassCastException ----类型转换异常
//错误的向下转型 Object obj = new String(); Integer integer = (Integer) obj; System.out.println(integer);
- NullPointerException ----空指针异常
//传递的对象为空(null) public static void main(String[] args) { method(null); } public static void method(String str) { System.out.println(str.length()); }
14.3 异常处理机制
1、 Java程序在执行过程中如果出现异常,会自动生成一个异常类对象,该异常对象将被自动提交给JVM,这个过程称为抛出**(throw)**异常。
2、 当JVM接收到异常对象时,会寻找能处理这一异常的代码并把当前异常对象交给其处理,这一过程称为捕获**(catch)**异常和处理异常。
3、 如果JVM找不到可以捕获异常的代码,则运行时系统将终止,相应的Java程序也将退出。
14.3.1 异常处理1—try catch
结构:
try{
… //可能产生异常的代码
}catch( ExceptionName1 e ){
… //异常的处理代码
}catch( ExceptionName2 e ){
… //异常的处理代码
} finally{
… //无论如何都会执行的语句 (常用来关闭资源)
}
Scanner scan = new Scanner(System.in); System.out.println("请输入被除数:"); int x = scan.nextInt(); System.out.println("请输入除数:"); int y = scan.nextInt(); try { System.out.println(x / y); System.out.println("计算结束!"); } catch (ArithmeticException e) { System.out.println("处理算数异常"); } finally{ scan.close(); } /* 请输入被除数: 12 请输入除数: 0 处理算数异常 */
注:
- catch语句可以添加多个,但必须按照捕获异常的范围从小到大依次出现(看两个异常类是否有继承关系。)Exception作为所有异常的父类,出现时,需要在最后的catch语句。
- finally语句,可依据需要添加,也可以不添加,语句内通常是用于关闭资源。
- JDK新特性 ,一个catch代码块可以捕获多个异常,在他们之间使用 “|” 分隔( catch( ExceptionName1 | ExceptionName2 e) )
- 抛出异常之后,try中的语句将不再实现。
14.3.2 异常处理2—throws
当本身程序处理不了,就会向上抛出异常类型,一层一层向上抛出,如果始终不处理,最终抛到JVM处理。
结构:public 返回值类型 方法名(参数列表) throws 异常类{
}
main方法向上抛出,直接抛给JVM。
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { //获取A类的字节码文件对象 Class<?> c = Class.forName("day.dream.test.A"); A a=(A)c.newInstance(); }
非主方法抛出异常,向上抛给调用方
public static void main(String[] args) { //此处处理异常 try { method02(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //抛出异常,将异常交给调用方处理 private static void method02() throws ClassNotFoundException { method01(); } //抛出异常,将异常交给调用方处理 public static void method01() throws ClassNotFoundException{ Scanner scan = new Scanner(System.in); System.out.println("请输入类路径:"); String classPath = scan.next(); Class<?> c = Class.forName(classPath); System.out.println(c); scan.close(); }
14.3.3 异常处理3—throw
手动抛出异常,需要使用到自定义异常,详细示例见14.4
14.4 自定义异常类
14.4.1 创建自定义异常
创建自定义异常,需要继承Exception 或其子类
示例:创建自定义异常类抛出除数不合法的异常
public class MyException extends Exception{ @Override public String toString() { return "当前除数为不合法"; } }
14.4.2 使用自定义异常
使用自定义异常类时,需要与throw结合,手动抛出符合自定义的异常。
示例:
Scanner scan = new Scanner(System.in); System.out.println("请输入被除数:"); int x = scan.nextInt(); System.out.println("请输入除数:"); int y = scan.nextInt(); try { if(y<=0) throw new MyException(); //手动抛出自定义异常 System.out.println(x / y); System.out.println("计算结束!"); } catch (MyException e) { System.out.println(e.toString()); //异常处理 } finally{ scan.close(); } /* 请输入被除数: 12 请输入除数: 0 当前除数为不合法 */