第2章 异常
一、异常介绍和体系介绍指的是程序在编译或执行过程中出现的非正常的情况 (错误)ArrayIndexOutOfBoundsExceptionClassCastExceptionNullPointerException ...注意 : 语法错误, 不是异常.所有的错误和异常都继承于Throwable类Error严重级别问题常见的 : 栈内存溢出 (StackOverflowError)堆内存溢出 (OutOfMemoryError)ExceptionRuntimeException 及其子类运行时异常除RuntimeException 之外所有的异常编译时异常堆内存溢出举例内存装不下了package com.itheima.error; public class OOMTest { public static void main(String[] args) { int[] arr new int[Integer.MAX_VALUE]; } }运行时异常数组索引越界异常: ArrayIndexOutOfBoundsException空指针异常 : NullPointerException类型转换异常ClassCastException说明编译阶段没有错误运行时 [可能] 会出现的错误这种错误通常都是程序员代码不严谨所造成的编译时异常编译阶段就出现的错误主要起到提醒作用二、Java程序异常默认处理流程阅读异常的处理顺序是从下往上看异常默认的处理方式: 向上抛出① 会在出现异常的位置, 创建一个异常对象 new ArithmeticException();② 将异常对象向上抛出, 抛给main方法③ main方法接收到异常对象, 继续向上抛出 new ArithmeticException();④ JVM虚拟机接收到异常对象, 将异常的错误信息打印在控制台, 将程序停止.package com.itheima.exception; public class ExceptionDemo1 { /* 异常默认的处理方式: 向上抛出 */ public static void main(String[] args) { System.out.println(main方法开始执行...); method(); // ③ main方法接收到异常对象, 继续向上抛出 new ArithmeticException(); // ④ JVM虚拟机接收到异常对象, 将异常的错误信息打印在控制台, 将程序停止. System.out.println(main方法执行结束...); } private static void method() { System.out.println(method方法开始...); int i 1 / 0; // ① 会在出现异常的位置, 创建一个异常对象 new ArithmeticException(); // ② 将异常对象向上抛出, 抛给main方法 System.out.println(method方法结束...); } }三、异常处理方式1-try...catchThrowable的常见方法public String getMessage()获取异常的错误原因public void printStackTrace()展示完整的异常错误信息package com.itheima.exception; public class ExceptionDemo2 { /* 异常处理方式1 try { ... } catch (异常类名 对象名) { ... } 执行流程: 1. 执行try语句中的代码, 看是否有异常发生 2. 有的话, catch捕获异常, 执行内部的异常处理代码 3. 没有的话, 不会执行catch内部代码, 程序继续执行. */ public static void main(String[] args) { System.out.println(开始); try { int i 1 / 1; int[] arr new int[10]; System.out.println(arr[10]); } catch (ArithmeticException e) { // ArithmeticException e new ArithmeticException(); System.out.println(捕获了运算异常...); } catch (NullPointerException e) { // NullPointerException e new NullPointerException(); System.out.println(捕获了空指针异常...); } catch (Exception e) { // Exception e new ArrayIndexOutOfBoundsException(); String message e.getMessage(); System.out.println(message);//获取异常的错误原因 e.printStackTrace();//展示完整的异常错误信息 System.out.println(捕获了其它异常...); } System.out.println(结束); } }四、try...catch练习package com.itheima.test; import com.itheima.exception.StudentAgeException; import com.itheima.pojo.Student; import java.util.Scanner; public class ExceptionTest { /* 键盘录入学生的姓名和年龄, 封装为学生对象并打印. 异常的两种处理方式: 1. try...catch捕获异常 2. 抛出异常 使用思路: 看问题是否需要暴露 要: 抛出异常 不要: try...catch捕获. */ public static void main(String[] args) { Scanner sc new Scanner(System.in); Student stu new Student(); System.out.println(请输入学生姓名: ); String name sc.next(); stu.setName(name); while (true) { try {//选中想要try catch的代码ctrlaltT产生try catch环绕 System.out.println(请输入学生年龄: ); int age Integer.parseInt(sc.next());//包装类的转换方法将数字字符串转换为数字,若输入有误就会抛出NumberFormatException stu.setAge(age); break; } catch (NumberFormatException e) { System.out.println(您输入的年龄有误, 请检查!); } catch (StudentAgeException e) { System.out.println(e.getMessage()); } } System.out.println(stu); } }五、异常处理方式2-throws抛出throw : 用在方法中, 后面跟的是异常对象,用于真正的抛出异常.throws : 用在方法声明上, 后面跟的是异常类名, 作用是声明,声明方法中的异常做抛出处理.细节: 如果抛出的异常对象, 是运行时异常, 就不需要编写throws, 反之必须写.在student类中package com.itheima.pojo; import com.itheima.exception.StudentAgeException; public class Student { private String name; private int age; //空参有参构造 //get、set方法 /* throw : 用在方法中, 后面跟的是异常对象, 用于真正的抛出异常. throws : 用在方法声明上, 后面跟的是异常类名, 作用是声明, 声明方法中的异常做抛出处理. 细节: 如果抛出的异常对象, 是运行时异常, 就不需要编写throws, 反之必须写. */ public void setAge(int age) {//运行时异常不需要写throws if (age 0 age 100) { this.age age; } else { throw new StudentAgeException(年龄有误, 请检查是否是0~100之间的!); } } //toString方法 }六、自定义异常1、自定义编译时异常定义一个异常类继承Exception.重写构造器2、自定义运行时异常定义一个异常类继承RuntimeException.重写构造器。上面的age的get方法使用的是自定义异常需要写一个异常类package com.itheima.exception; public class StudentAgeException extends RuntimeException { public StudentAgeException() { } public StudentAgeException(String message) { super(message); } }