1.认识异常(Exception)
异常也是对象,是可以new的
程序运行不正常的
处理程序中不正常的手段 异常处理
1.1、简单的几种异常举例:
各种异常的案例:
public class Test2 {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数字:");
String num1s = scanner.nextLine();
System.out.println("请输入第二个数字:");
String num2s = scanner.nextLine();
int num1 = Integer.parseInt(num1s);
int num2 = Integer.parseInt(num2s);
System.out.println(num1/num2);
}catch (NumberFormatException e){ // 兼容
System.out.println("完蛋了,您输入的不是数字");
}catch (ArithmeticException e){
System.out.println("除数不能为0");
}catch (Exception e){
System.out.println("未知异常!!!!!!!");
System.exit(-1);
}finally {
System.out.println("程序结束!");
}
/*
程序结束
*/
}
}
在异常中获得三个重要信息:异常名,异常描述信息,异常位置
异常名:ArithmeticException
异常描述信息:/ by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.bigdata09.day23.exception.Test.main(Test.java:24)
异常名:NumberFormatException(数字格式转换异常)
异常描述信息:For input string: “4e”
Exception in thread "main" java.lang.NumberFormatException: For input string: "4e"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at com.bigdata09.day23.exception.Test.main(Test.java:22)
2.异常处理
使用几个关键字try
catch
finally
throw
throws
1.try
:监视可能会出现异常的代码
2.catch
:监视的代码出现异常 ,可以捕获到这个异常对象的引用,catch捕获的异常类型必须与实际发生的异常类型一致或者兼容。catch
在try
之后可以出现n次,catch
的先后顺序一定要注意,兼容异常Exception
一定要放在最后面。
3.finally
: 是最终的意思,是否发生异常,异常是否被捕获都会执行。出现System.exit(-10);
不再执行
System.exit(-10); : 表示结束JVM
4.throw
: 人为抛出异常 (检查时异常)
5.throws
: 方法声明会抛出哪些异常
引发多种类型的异常
- 排列catch 语句的顺序:先子类后父类
- 发生异常时按顺序逐个匹配
- 只执行第一个与异常类型匹配的catch语句
尝试了自定义异常
异常代码案例
public class Test3 {
public static void main(String[] args) {
int num1 = 0;
try {
num1 = MyInput.getNum("请输入第一个数字:");
} catch (NoShuZiException e) {
e.printStackTrace(); // 打印原始的异常
System.out.println("输入的第一个数字不对,程序结束");
return;
}
int num2 = 0;
try {
num2 = MyInput.getNum("请输入第二个数字:");
} catch (NoShuZiException e) {
System.out.println("输入的第二个数字不对,程序结束");
return;
}
System.out.println(num1/num2);
}
}
public class MyInput {
// 静态常量
public static final Scanner input = new Scanner(System.in);
// 静态方法 :与对象无关,通过类名来调用
public static int getNum(String s) throws NoShuZiException {
System.out.println(s);
try {
int num = Integer.parseInt(input.nextLine()); // 风险语句,不为数字时为异常
return num;
}catch (NumberFormatException e){ // 运行时异常
// 应该显示什么信息?信息在哪里显示?
throw new NoShuZiException(); // 人为封装异常并抛出 异常类型为:检查异常
//throw new NoShuZiException(); 只能抛出异常,不能解决异常
}
}
}
public class NoShuZiException extends Exception{
public NoShuZiException() {
}
public NoShuZiException(String message) {
super(message);
}
public NoShuZiException(String message, Throwable cause) {
super(message, cause);
}
public NoShuZiException(Throwable cause) {
super(cause);
}
public NoShuZiException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
3.异常类的继承体系结构
NumberFormatException
等
ArithmeticException
IllegalAccessError
检查异常
RuntimeException(运行时异常)
Exception
Erro
Throwable
运行时异常 和 检查异常
运行时异常:编译时没有提示
检查异常:编译时必须处理:try…catch 或者 throws
面试问题:
Java的类加载机制?
Java的字节码校验?