1.异常:
程序中出现的不正常现象。
2.异常的由来:
程序在运行的过程中出现了不正常的情况,程序把它看成对象提取了属性行为(名字,原因,位置等信息)
形成了各种异常类
3.异常的分类:(throwable)
1.Error(错误):运行中出现的严重错误,不需要我们进行更改。
2.Exception():运行中出现的不严重的错误,我们可以尝试去更改.
4.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()),这里没有处理异常的能力
//将异常向上抛,抛给它所在的方法
}
}
5.异常的特点:
当程序出现多个异常的时候,程序会打印异常信息并中断程序,所以当同时出现多个异常的时候,只能执行第一个.
6.常见的异常
public class Demo2 {
public static void main(String[] args) {
int[] arr = new int[]{4,5,6};
//数组下标越界异常 ArrayIndexOutOfBoundsException
//System.out.println(arr[7]);
arr = null;
//空指针异常 NullPointerException
System.out.println(arr[0]);
}
}
7.对异常的处理
检测异常,捕获异常,让异常不影响下面代码的执行.
* 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()
}
}
8.多异常的处理
* try{
* 可能出现异常的代码
* }catch(异常一 e){//捕获异常 e就是要捕获的异常
* 对出现异常的代码的处理过程
* }catch(异常二 e){//捕获异常 e就是要捕获的异常
* 对出现异常的代码的处理过程
* }catch(Exception e){//捕获异常 e就是要捕获的异常
* 对出现异常的代码的处理过程
* }
*
* 继续执行下面正常的代码
public static void main(String[] args) {
Math1 math1 = new Math1();
try {
math1.div(3, 0);
} catch (ArithmeticException e) {
e.printStackTrace();
}catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}catch (Exception e) {//注意:一定要将包含Exception的异常捕获放后面,不然后面的异常执行不到了父类异常所有异常都能接收
e.printStackTrace();
}
System.out.println("go on");
}
}
class Math1{
int[] arr = {3,5};
public int div(int a, int b){
System.out.println(arr[1]);
return a/b;
}
}
* try{
* 可能出现异常的代码
* }catch(Exception e){//捕获异常 e就是要捕获的异常
* 对出现异常的代码的处理过程
* }finally{
* 必须执行的代码:作用:用于资源的释放:比如:多线程中的锁对象,流的关闭,数据库的关闭等
* }
*
*
*
* 继续执行下面正常的代码
*
* try{
* 获取资源
* }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()
}
}
9.自定义异常
自定义异常:自己定义的异常类,由于Exception里面有异常的基本功能,一般我们都写Exception的子类
*
* 为什么要有自定义异常?
* 答:系统没有定义的异常需要我们自己定义,我们解决的是系统没有解决的问题.
*
* 比如:订单异常 用户信息异常 除数为负数
*
* 异常的分类:
* 编译异常:在编译阶段抛出,处理的异常---除RuntimeException以外的所有异常
* 所有相关的工作都要由我们自己完成
* 运行时异常:在运行阶段抛出,处理的异常--RuntimeException异常
* 所有的工作我们都可以不管
*
* 异常处理的方式:
* 1.异常的声明
* 异常声明后,调用者去处理,调用者不处理,继续声明直到交给JVM
* 2.trycatch
* 实现的是真正的对异常的处理
*
*
* 让谁去处理异常比较合适?
* 谁调用可能出现异常的方法,谁负责处理异常
*/
//自定义一个负数异常类:
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;
}
}