1, 异常
定义: 程序中出现的问题
1.1, Error
系统级别的异常,跟程序员没关系
1.2, Exception
定义: 代表程序可能出现的问题,通常用Exception以及他的子类来封装程序出现的问题.
1.2.1, RuntimeException
定义: 运行时异常,编译阶段不会出现问题,运行时出现了问题
RuntimeException及其子类
特点: 运行时异常,在编译阶段不需要处理,(语法正确,IDEA不会提醒)
1.2.2, 其他异常
定义: Exception里除了RuntimeExcept都是编译时异常
特点: 编译时必须要手动处理,否者代码报错,(语法错误,IDEA直接提醒)
1.3, 异常的作用
- 查看bug的关键参考信息
class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public class Demo1 { public static void main(String[] args) { Student[] arr = new Student[3]; String str = arr[0].getName(); System.out.println(str); } }
异常信息: (空指针异常)
Exception in thread "main" java.lang.NullPointerException at Test.Demo1.main(Demo1.java:35)
- 可以作为方法内部的一种特殊返回值,以便通知调用者底层执行情况 [🔺🔺🔺]
class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if(age<18||age>30){ throw new RuntimeException(); } this.age = age; } } public class Demo{ public static void main(String[] args){ Student s = new Student(); s.setAge(40); } }
1.4, 异常的处理方式
1.4.1, JVM默认的处理方式
- 把异常的名称,异常原因及异常出现的位置等信息输出在控制台
- 程序停止执行,下面的代码不会再执行
1.4.2, 异常捕获
格式1:
try{ 可能异常语句块; } catch (出现的异常类名 e){ 异常处理语句; } System.out.println("我仍然执行");
注意:
- try中没有遇到问题,不会执行catch里面的代码,try里面的代码正常执行,try{}case{}后面的代码正常执行
- try中遇到多个问题,需要catch抛出多个异常(用 | 隔开),或多个catch抛出异常
1.4.3 throws 异常的捕捉(提示)
throws 告知调用者此处有异常,注意抛出
public static 类 throws Exception{ }
1.4.4 throw 关键字 (抛出异常)
throw 造错 可以指定错误异常提示 (异常抛出)
import java.util.Scanner; public class Dome02 { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); System.out.println("输入用户名:"); String username = sc.next(); if(!username.equals("狗蛋") ){ throw new Exception("用户名不存在"); } System.out.println("请输入密码:"); String password = sc.next(); if(!password.equals("13321134567")){ throw new Exception("密码错误"); } System.out.println("登陆成功!"); } }
1.4.5 throw和throws的区别[※※※重要]
throw:
1. 表示方法内抛出某种异常对象 2. 如果异常对象是非 RuntimeException 则需要在方法申明时加上该异常的抛出 即需要加上 throws 语句 或者 在方法体内 try catch 处理该异常,否则编译报错 3. 执行到 throw 语句则后面的语句块不再执行
throws:
1. 方法的定义上使用 throws 表示这个方法可能抛出某种异常 2. 需要由方法的调用者进行异常处理
1.4.6 自定义异常
示例:
import java.util.Scanner; class SinglerException extends Exception{ public SinglerException(){ super(); } public SinglerException(String message){ super(message); } } public class Demo03 { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); System.out.println("请输入是否是情侣(true/false):"); buy(sc.nextBoolean()); } public static void buy(boolean issSingle) throws Exception{ if(issSingle){ throw new SinglerException("单身不能购买"); } System.out.println("情侣买一送一"); } }