异常处理
常见的异常
- 1.空指针异常
- 2.越界异常
- 3.算术异常
异常处理方式:
- 1.自己处理
- 2.交给上级处理
Jvm虚拟机处理
- 1.打印错误信息
- 2.打印异常类
- 3.打印错误发生的位置
- 4.将程序停止
异常类
Throwable(所有异常的父类)
子类:
1.Error(例如:数据库崩溃 系统崩溃)
2.Exception(异常类)
常见的异常
int[] arr = {1,2};
1.空指针异常
arr = null;
System.out.println(arr[0]);
2.越界异常
System.out.println(arr[2]);
3.算术异常
int a = 10 / 0;
异常处理方式:
try…catch…finally
try:存放可能发生异常的代码
catch:匹配异常类的对象
finally:异常匹配完毕后 肯定会执行的代码
使用try处理
- 1.程序不会停止
- 2.catch成功后 执行括号中语句
- 3.执行括号中语句执行完毕 程序继续执行
用try…catch处理异常的流程
- catch处理异常 可以使用多个catch
- try中 可能发生多个类型的异常
- 这时可以用 多个catch进行匹配
- 匹配时注意 类要从小往大依次书写
例:
//测试异常处理
class TestException{
public int fun(int a,int b) {
/*
* 10/0发生算术异常
* 产生一个异常对象
* new ArithmeticException(“/by zero”)
* 将这个创建好的异常对象 返回给方法的调用者
*/
return a / b;
}
}public static void main(String[] args) {
TestException test = new TestException();try {
//new ArithmeticException(“/by zero”)
//异常对象返回 int类型接收不了
//异常对象会向下与catch进行匹配
//匹配成功 执行catch后面括号里的代码
//异常处理完毕 不会停止程序 会向下继续执行
int fun = test.fun(10, 0);
//注意:如果发生异常 下面的代码不会执行
//直接开始匹配catch
System.out.println(fun);
} catch (ArithmeticException e) {
//ArithmeticException e = new ArithmeticException(“/by zero”)
// 要匹配的异常类
System.out.println(“catch”);
} catch (NullPointerException e) {
System.out.println(“空指针”);
} catch (Exception e) {
System.out.println(“异常的父类”);
}
System.out.println(“程序停止”);
}
finally
- 无论是否发生异常
- finally中的语句一定会被执行
- 作用:
- 数据库部分 用于关闭数据库
- 流部分 关闭流资源
-
- final finally fianlize 三个关键词没有关系
-
- final 修饰变量 表示不可更改
- finally 异常处理部分使用 关闭资源
- finalize 方法名 Object类中
throw 后面跟着 抛出的异常对象
* throws 后面 跟着 异常类的 类名
* 位置:
* throw: 方法中
* throws:方法上
* 作用:
* throw:抛出一个异常对象
* throws:标识方法携带一个异常
例1:
//控制年龄范围
//抛出异常的方法 需要跟方法上表示一下
//该方法可能会接受到异常对象
//关键字 throws 标识方法 有异常
public void setAge(int age) throws Exception {
if (age > 0 && age < 120) {
this.age = age;
}else {
//抛出一个异常对象
//提示用户 赋值错误
//Exception e = new Exception(“AgeOutOfBoundsException age:” + age );
//谁调用了该方法 就将该异常对象抛给调用者
//关键词 throw 抛
//创建自定义异常类的对象
AgeOutOfBoundsException e = new AgeOutOfBoundsException("AgeOutOfBoundsException age:" + age);
throw e;
}
}
例2:
//自定义异常类
//核心:类名(见名知意 类名就能反应出异常类型)
//年龄超出范围
class AgeOutOfBoundsException extends Exception {
private static final long serialVersionUID = 1L;
public AgeOutOfBoundsException() {
// TODO Auto-generated constructor stub
super();
}
public AgeOutOfBoundsException(String message) {
super(message);
}}
异常的分类
1.运行时异常
(程序员犯的错 需要将程序停下来 修改错误)
例如:空指针 越界
特点:
- a.不强制处理
- b.一旦发生运行时异常 程序会立即停止(不处理的情况下)
2.编译时异常(除了运行时异常剩下的全是编译时异常)
- 未雨绸缪(有可能发生错误 提前为这个错误做准备)
- 特点:系统强制你对编译时异常进行处理
例1:
//抛编译时异常
public void fun1() throws Exception {
throw new Exception(“编译时异常”);
}
例2:
//抛运行时异常
//注意: 方法后的throws声明 可以省略
public void fun2() {
throw new RuntimeException(“运行时异常”);
}
继承中的异常(多线程)
例:
class Father{
//父类中的方法 抛出一个编译异常
public void fun() throws Exception {
}
}
class Son extends Father{
//重写父类方法
//1.子类重写父类异常方法 可以选择抛不抛出异常
//2.父类方法没有抛出异常时 子类重写方法中
//不能抛出异常 只能选择自己处理这个异常
@Override
public void fun() throws Exception {
// TODO Auto-generated method stub
test();
}
//带异常的方法
public void test() throws Exception {
}
}