异常:
解释:程序中出现的不正常的情况。
异常的由来:程序运行的过程中出现了不正常的情况,程序把它看成了对象,提取了属性,行为(名字,原因,位置等),形成了各种异常类。
异常的分类:(throwable)
- Error:(错误)运行中出现的严重错误,不需要我们进行更改。
- Exception:(异常)运行中出现的不严重的错误,我们可以尝试去更改。
Exception分类:
- 系统异常:系统提前定义好的,我们直接使用。
- 自定义异常:需要我们自己定义。
- 编译异常:在编译阶段抛出异常,处理异常。
- 运行时异常,在运行阶段抛出异常,处理异常。
public class Demo28 {
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.创建除数为0的异常对象(new ArithmeticException()),这里没有处理异常的能力,将 异常向上抛,抛给它所在的方法
}
public static double sqrt(double temp) {
// TODO Auto-generated method stub
return 0;
}
}
异常的特点:当程序出现异常的时候,程序会打印异常信息中并中断异常程序。所以当同时出现多个异常的时候只能执行第一个
对异常的处理:检测异常
对异常的处理:检测异常
try{
可能出现异常的代码
}catch(Exception e){//捕获异常 e就是要捕获的异常
对出现异常的代码的处理过程
继续执行下面正常的代码
单异常 的处理:
public class Demo02 {
public static void main(String[] args) {
Math math = new Math();
try {
int value = math.div(4, 0); //3.抛到这里
//只要try内部的代码发生了异常,catch会立刻捕获异常,所以这里的代码不会执行下面的value输出
//只有try里面的代码没有发生异常,这里的代码才会执行。
System.out.println("value = "+value);
} catch (Exception e) {//4.捕获异常 e = new ArithmeticExcepiton
//e.printStackTrace();//打印异常的位置,原因,名字等信息
System.out.println("处理异常的代码");
System.out.println(e.getMessage());//异常的原因
System.out.println(e.toString());//异常的名字,原因。
}
System.out.println("go on");
}
}
class Math {
public int div(int a,int b) { //2.抛到这里
return a/b; //1.产生并抛出
}
}
多异常的处理:
try{
可能出现异常的代码
}catch(异常一 e){//捕获异常 e就是要捕获的异常
对出现异常的代码的处理过程
}catch(异常二 e){
}catch(Exception e){
}
继续执行下面正常的代码
public class Demo03 {
public static void main(String[] args) {
Math1 math1 = new Math1();
try {
math1.div(3, 0);
} catch (ArrayIndexOutOfBoundsException e) {//捕获下标越界异常
// TODO: handle exception
e.printStackTrace();
}catch( ArithmeticException e) {//捕获的出除数为零的异常
// TODO: handle exception
e.printStackTrace();
}catch(Exception e) {//注意:一定要将包含Exception 的异常捕获防砸所有异常捕获的最后面
e.printStackTrace();
}
System.out.println("go on");
}
}
class Math1 {
public int div(int a,int b) { //2.抛到这里
int [] arr = new int [] {3,4};
System.out.println(arr[1]);
return a/b; //1.产生并抛出
}
}
try---finally结构和try--catch---finally结构:
try{
可能出现异常的代码
}catch(Exception e){//捕获异常 e就是要捕获的异常
对出现异常的代码的处理过程
}finally{
必须执行的代码:作用:用于资源的释放 ,比如:多线程中的锁对象,流的关闭,数据库的关闭 等
}
继续执行下面正常的代码
try——finally结构
try{
用于获取资源
}finally{
释放资源:
必须执行的代码:作用:用于资源的释放 ,比如:多线程中的锁对象,流的关闭,数据库的关闭 等
}
public class Demo04 {
public static void main(String[] args) {
Math2 math2 = new Math2();
try {
math2.div(22, 0);
} catch (ArithmeticException e) {
// TODO: handle exception
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.抛到这里
return a/b; //1.产生并抛出
}
}
自定义异常:
自己定义的异常类,由于Exception里面有异常的基本功能,一般我们都去写Exception的子类
为什么要有自定义异常?
答:系统没有定义的异常需要我们自己定义,所以我们解决的是系统没有解决的问题 。比如:订单异常,用户信息异常,比如我们认为除数是负数就是异常
遗产的分类:
编译异常:在编译阶段抛出,处理的异常---除RuntimeException以外的所有异常,所有相关的工作都要由我们自己完成
运行时异常:在运行阶段抛出。处理的异常---RuntimeException异常。所有相关的工作我们都可以不管。
异常的处理方式:
- 异常的声明:异常声明后,调用者去处理,调用者不处理,继续声明知道交给JVM
- try--catch 实现的是真正的对异常的处理
//自定义的异常类 class FuShuException extends Exception{ public FuShuException() { } public FuShuException(String message) { super(message); } } public class Demo05 { public static void main(String[] args) { Math3 math3 = new Math3(); //1.try-catch try { math3.div(3, -2); } catch (FuShuException e) { // TODO: handle exception e.printStackTrace(); } //2.继续声明 throws FuShuException //math3.div(3, -2); } } class Math3 { /* * throw是抛出的意思 * throws 是声明异常的意思 * */ //声明异常,告诉别人我有可能发生异常 public int div(int a,int b) throws FuShuException { if (b<0) { throw new FuShuException("除数为负数");//手动生成并抛出除数为负数的异常 } return a/b; } }
实例:老师用电脑上课
/*
* 使用自定义的异常类
*
* 老师用电脑上课
* 老师发生上课异常
* 上课时电脑发生蓝屏或者冒烟异常
*
*
*
*/
public class Demo07 {
public static void main(String[] args) {
Teacher1 teacher1 = new Teacher1(new Computer());
try {
teacher1.work();
} catch (ShangKeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("去中关村修电脑");
}
}
}
class Teacher1 {
Computer computer;
public Teacher1(Computer computer) {
super();
this.computer = computer;
}
public Teacher1() {
super();
// TODO Auto-generated constructor stub
}
// 老师上课
public void work() throws ShangKeException{
try {
computer.computerWork();
} catch (LanPingException e) {
e.printStackTrace();
computer.reset();
} catch (MaoYanException e) {
e.printStackTrace();
throw new ShangKeException("老师无法继续上课");
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Computer {
// 设定当前的状态值:代表当前要发生的异常。1.蓝屏,2.冒烟
int status = 2;
// 重启方法
public void reset() {
System.out.println("重启电脑");
}
// 电脑工作
public void computerWork() throws LanPingException, MaoYanException {
switch (status) {
case 1:// 蓝屏
throw new LanPingException("蓝屏了");
case 2:// 冒烟
throw new MaoYanException("冒烟了");
}
}
}