异常:顾名思义就是程序运行时候出现不正常情况。
一、异常分类、异常概念
- Error:非常严重的错误,联想到人就是得了不治之症。
- Exception:并不是非常严重的错误,联想到人得了感冒这种疾病,通过吃药可以治疗。

ArrayIndexOutofBoundsException 数组下标越界异常
NullPointerException 空指针异常
StringIndexOutofBoundsException 字符串下标越界异常 Char charAt(int index)
ArithmeticException 算数异常
ClassCastException
大部分以able结尾的一般都是接口,able在英语里面就是表达能不能意思,但是Throwable是类不是接口比较特殊。
public class Throwable
异常的由来:Java是纯面向对象语言,异常就是Java用面向对象的思想将不正常的情况进行了封装。
如果访问了数组不存在的下标,java就把这个错误信息封装到ArrayIndexOutOfBoundsException这个类里面。
二、异常的处理
try {
需要检查的代码(可能会抛出异常,也可能不会抛出异常)
} catch(异常的类型 异常类型的变量) {
捕获异常后要处理异常
} finally {
一定会被执行的代码(不管异常抛不抛出都会执行)
}
@Test
public void test2() {
//运行时异常:运行的时候抛出的异常,是程序员自己写的代码有问题
//java.lang.ArithmeticException: / by zero
//int num = 1 / 0;
String str = null;
System.out.println(str.toUpperCase());
//编译时异常:未雨绸缪
//Unhandled exception: java.io.FileNotFoundException
try {
// 需要检查的代码(可能会抛出异常,也可能不会抛出异常)
FileInputStream fileInputStream = new FileInputStream("a.txt");
System.out.println("ExceptionDemo.test2 try");
} catch (FileNotFoundException e) {
//捕获异常后要处理异常
e.printStackTrace();
} finally {
//一定会被执行的代码(不管异常抛不抛出都会执行)
System.out.println("finally");
}
System.out.println("ExceptionDemo.test2");
}
三、编译时异常(非运行时异常、检查异常)
编译时异常在写代码时候必须要处理这个异常,不处理就报错。
处理的方法有两种:
1、try-catch捕获这个异常,自己处理了这个异常
2、throws抛出异常,我不处理这个异常,抛出异常
@Test
public void test31() {
//Unhandled exception: java.lang.ClassNotFoundException
// try-catch自己处理这个异常
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void test55() {
try {
show();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//抛出编译时异常,谁调用我谁去处理
public void show() throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
}
四、多重捕获块(多个catch)
try {
需要检测的代码(可能会抛出异常,也可能不会抛出异常)
} catch (异常的类型1 异常类型的变量1) {
捕获异常后处理异常
} catch (异常的类型2 异常类型的变量2) {
捕获异常后处理异常
} catch (异常的类型3 异常类型的变量3) {
捕获异常后处理异常
} finally {
一定会被执行的代码(不管异常抛不抛出都会执行,例如数据库释放连接)
}
@Test
public void test45() {
try {
FileInputStream fileInputStream = new FileInputStream("a.txt");
fileInputStream.read();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("finally");
}
//FileNotFoundException extends IOException
try {
FileInputStream fileInputStream = new FileInputStream("a.txt");
fileInputStream.read();
} catch (IOException e) {
e.printStackTrace();
} /*catch (FileNotFoundException e) {
//Exception 'java.io.FileNotFoundException' has already been caught
e.printStackTrace();
}*/ finally {
System.out.println("finally");
}
}
五、自定义异常
自定义异常:
1、所有的异常的都是Throwable的子类
2、如果写一个运行时异常,需要继承RuntimeException
3、如果要写一个编译时异常,继承Exception
案例:模拟银行转账,可以实现存钱和取钱的功能
取钱时候如果余额不够就抛出异常 MeiQianException
public class MeiQianException extends Exception{
//message代表抛出这个异常要打印的错误信息
public MeiQianException(String message) {
super(message);
}
}
@Test
public void test22() {
try {
quQian(1900);
} catch (MeiQianException e) {
e.printStackTrace();
}
}
public void quQian(double money) throws MeiQianException {
if (money >= 1000) {
throw new MeiQianException("钱不够");
}
System.out.println("钱够了");
}
六、throws、throw
throws是在方法签名上
throw是在代码中抛出异常
public void quQian(double money) throws MeiQianException {
if (money >= 1000) {
throw new MeiQianException("钱不够");
}
System.out.println("钱够了");
}