#异常
定义:程序中出现的不正常的情况
异常的由来:
- 程序在运行的过程中出现的不正常的情况,程序把它看成对象,提取了属性,行为(名字,原因,位置等信息),形成了各种异常类。
异常的分类(throwable)
- Error:(错误):运行中出现的严重错误,不需要我们进行更改.
- 注意:出现这个错误的时候,程序直接就错误结束了,我们没有机会去改正。
- Exception:运行中出现的不严重的错误,我们可以尝试去更改.
- 注意:出现这个异常的时候,我们可以根据程序所报的错来调试和改正程序,知道程序正确运行。
Exception的分类
- 第一种分类:
- 系统异常:系统提前定义好的,我们直接使用。
- 自定义异常:需要我们自己定义。
- 第二种分类:
- 编译异常:在编译阶段抛出异常,处理异常。
- 运行时异常:在运行阶段抛出异常,处理异常。
异常的特点
- 异常的特点:当程序出现异常的时候,程序会打印异常信息并中断程序。
- 所以当同时出现多个异常的时候,只能执行第一个。
- 一个简单的异常:
- 实例代码:
public class Demo8 {
public static void main(String[] args) {//4.这里也没有处理异常的能力,继续往上抛,抛给JVM(java虚拟机),JVM的处理办法:就是调用异常对象的打印方法将异常的名字,位置,原因打印到控制台.
Math math = new Math();
math.div(2, 0);//3.抛给了这里,这里也没有处理的能力,继续往上抛,抛给main。
}
}
class Math{
public int div(int a,int b){//2.抛给了这里,这里也没有处理的能力,继续往上抛,抛给调用方法的位置。
return a/b;//1.先创建除数为零的异常对象(new ArithmeticException()),这里没有处理异常的能力,将异常向上抛,抛给他所在的方法。
}
}
异常的处理
- 处理模式:检测异常,捕获异常,让异常不影响下面代码的执行
- 处理框架:
try{
可能出现异常的代码
}catch(Exception e){//捕获异常 e就是要捕获的异常
对出现异常的代码的处理过程
}
继续执行下面的代码
- 实例代码:
public class Demo3 {
public static void main(String[] args) {
Math math = new Math();
try {
int value = math.div(4, 0);//3.抛到这里 new ArithmeticException()
//只要try内部的代码发生了异常,catch会立刻捕获异常,所以这里的代码不会执行.
//只有try里面的代码没有发生异常,这里的代码才会执行.
System.out.println("value:"+value);
} catch (Exception e) {//4.捕获异常: e = new ArithmeticException()
//e.printStackTrace();//可以打印异常的位置,原因,名字等信息
System.out.println(e.getMessage());//异常的原因
//System.out.println(e.toString());//异常的名字,原因
System.out.println("处理异常的代码");
}
System.out.println("go on");
}
}
class Math{
public int div(int a,int b) { //2.抛到这里 new ArithmeticException()
return a/b;//1.产生并抛出 new ArithmeticException()
}
}
多异常讲解
- 定义:一次性抛出了多个异常
- 异常的捕获和处理方式:
try{
可能出现异常的代码
}catch(异常一 e){//捕获异常 e就是要捕获的异常
对出现异常的代码的处理过程
}catch(异常二 e){//捕获异常 e就是要捕获的异常
对出现异常的代码的处理过程
}catch(Exception e){//捕获异常 e就是要捕获的异常
对出现异常的代码的处理过程
}
继续执行下面的代码
finally讲解
- 解释:finally里一般拿来做一些善后清理工作try块里出现错误的话,会立即跳出try块,找到匹配的错误,执行catch块里的语句此时,可能在try块里打开的文件没关闭,接的网络没断开,对这些浪费的内存就不能及时释放回收。如果有finally块的话,不管有没有出错,都会执行finally块里的内容。
- 作用:用于资源的释放,比如多线程中的锁对象,流的关闭,数据库的关闭等。
- 使用方法:
try{
可能出现异常的代码
}catch(Exception e){//捕获异常 e就是要捕获的异常
对出现异常的代码的处理过程
}finally{
必须执行的代码
}
继续执行下面正常的代码
- 给一个具体的例子:
public class Demo5 {
public static void main(String[] args) {
Math2 math2 = new Math2();
try {
math2.div(22, 0);
} catch (ArithmeticException e) {
e.printStackTrace();
//return;//让当前的方法结束,finally里面的代码还是可以执行
System.exit(0);//退出程序,finally里面的代码不会再执行
} finally {
//必须执行的代码
System.out.println("finally");
}
System.out.println("go on");
}
}
class Math2{
public int div(int a,int b){//2.抛到这里 new ArithmeticException()
int[] arr = {3,4};
System.out.println(arr[1]);
return a/b;//1.产生并抛出 new ArithmeticException()
}
}
自定义异常
- 定义:自己定义的异常类,由于Exception里面有异常的基本功能,一般我们都写Exception的子类。
- 自定义异常的原因:系统没有定义的异常需要我们自己定义,我们解决的是系统没有解决的问题。
- 自定义的异常的分类:
- 在编译阶段抛出,处理的异常—除RuntimeException以外的所有异常所有相关的工作都要由我们自己完成。
- 在运行阶段抛出,处理的异常–RuntimeException异常所有的工作我们都可以不管。
- 异常的处理机制:谁调用可能出现异常的方法,谁负责处理异常。
- 自定义异常的过程实例代码:
//一、自定义的异常类
class FuShuException extends Exception {
public FuShuException() {
}
public FuShuException(String message) {
super(message);
}
}
public class Demo6 {
public static void main(String[] args) {
Math3 math3 = new Math3();
//四、两种方式(1.trycatch 2.继续声明)
try {
math3.div(3, -2);//这里采用第一种方式。
} catch (FuShuException e) {
e.printStackTrace();//打印异常。
}
}
}
class Math3{
public int div(int a,int b) throws FuShuException{ //三、声明异常,告诉别人我有可能发生异常
/*
*throw是抛出的意思
*throws是声明异常的意思
*/
if (b<0) {
throw new FuShuException("除数为负数了");//二、手动生成并抛出除数为负数异常
}
return a/b;
}
}
最后来一个经典的异常的例子
- 说明:代码和问题都在其中说清楚了
public class Demo8 {
/*
* 使用自定义的异常类
* 老师用电脑上课
* 老师发生上课异常
* 上课时电脑发生蓝屏或者是冒烟异常
*/
public static void main(String[] args) {
Teachers teachers = new Teachers(new Computer());
try {
teachers.one();
} catch (TeachExcepting e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("去中关村修电脑");
}
}
}
class TeachExcepting extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public TeachExcepting() {
// TODO Auto-generated constructor stub
}
public TeachExcepting(String message) {
super(message);
}
}
class ComOneExcepting extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public ComOneExcepting() {
// TODO Auto-generated constructor stub
}
public ComOneExcepting(String message) {
super(message);
}
}
class ComTwoExcepting extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public ComTwoExcepting() {
// TODO Auto-generated constructor stub
}
public ComTwoExcepting(String message) {
super(message);
}
}
class Teachers {
Computer computer;
public Teachers(Computer computer) {
super();
this.computer = computer;
}
public Teachers() {
super();
// TODO Auto-generated constructor stub
}
//老师上课
public void one() throws TeachExcepting {
try {
computer.two();
} catch (ComOneExcepting e) {
// TODO: handle exception
e.printStackTrace();
computer.one();
} catch (ComTwoExcepting e) {
e.printStackTrace();
// TODO: handle exception
throw new TeachExcepting("老师无法上课");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
class Computer {
//设定状态值:代表要发生的异常 1:蓝屏; 2:冒烟
int flag = 2;
//重启方法
public void one() {
System.out.println("重启电脑");
}
//电脑工作
public void two() throws ComTwoExcepting, ComOneExcepting{
switch (flag) {
case 1:
throw new ComOneExcepting("电脑出现蓝屏异常");
case 2:
throw new ComTwoExcepting("电脑出现死机异常");
}
}
}
//在重写的方法中使用异常的注意点
/*
* 1.子类的同名的方法中声明的异常的等级要=<父类的。
* 2.子类的同名的方法中声明的异常可以与父类的不一致,也可以不声明异常,但是此时子类方法不能再抛出异常
* 3.如果子类同名方法声明了异常,父类必须声明异常。
*/
class BadComputer extends Computer {
@Override
public void two() throws ComTwoExcepting, ComOneExcepting {
// TODO Auto-generated method stub
super.two();
}
}
- 这个例子很好的结合了如何自定义一个异常,异常的处理机制,出现异常后代码的处理过程这几个问题共同来巩固了异常的知识。