异常

异常产生&异常处理

异常概述

什么是异常?Java代码在运行时期发生的问题就是异常。
在Java中,把异常信息封装成了一个类。当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如异常出现的位置、原因等)。
在Java中使用Exception类来描述异常。
这里写图片描述
查看API中Exception的描述,Exception 类及其子类是 Throwable 的一种形式,它用来表示java程序中可能会产生的异常,并要求对产生的异常进行合理的异常处理。
继续观察,我们可以发现Exception有继承关系,它的父类是Throwable。Throwable是Java 语言中所有错误或异常的超类,即祖宗类。

这里写图片描述
这里写图片描述
另外,在异常Exception类中,有一个子类要特殊说明一下,RuntimeException子类,RuntimeException及其它的子类只能在Java程序运行过程中出现。
这里写图片描述
我们再来观察Throwable类,能够发现与异常Exception平级的有一个Error,它是Throwable的子类,它用来表示java程序中可能会产生的严重错误。解决办法只有一个,修改代码避免Error错误的产生。
这里写图片描述
异常处理

JVM默认处理方式

如果出现异常我们没有处理,jvm会帮我们进行处理,他会把异常的类型,原因还有位置显示在命令行并且还终止了程序,异常后面的代码将不在执行

import java.io.FileWriter;
import java.io.IOException;


public class ExceptionDemo2 {
    public static void main(String[] args) throws Exception {
       System.out.println(2/0);
       System.out.println("hello");

    }
}

try…catch方式处理异常

捕获:Java中对异常有针对性的语句进行捕获,可以对出现的异常进行指定方式的处理捕获异常格式:

try {
    //需要被检测的语句。
}
catch(异常类 变量) { //参数。
    //异常的处理语句。
}
finally {
    //一定会被执行的语句。
}

try:该代码块中编写可能产生异常的代码。
catch:用来进行某种异常的捕获,实现对捕获到的异常进行处理。


import java.io.FileWriter;
import java.io.IOException;


public class ExceptionDemo2 {
    public static void main(String[] args) throws Exception {
        try {
            System.out.println(1);
            //System.out.println(2 / 0);
            System.out.println(2);
        } catch(ArithmeticException ae) {
            System.out.println("除数不能为0");
        }
        System.out.println(3);
    }
}

throws方式处理异常

A:throws使用:
权限修饰符 返回值类型 方法名(形参列表) throws 异常类型1,异常类型2….{
}

import java.io.FileWriter;
import java.io.IOException;


public class ExceptionDemo2 {
    public static void main(String[] args) throws Exception {
        //method();

        function();

    }

    public static void function() throws Exception {
        FileWriter fw = new FileWriter("a.txt");
    }

    private static void method() {
        try {
            System.out.println(1);
            //System.out.println(2 / 0);
            System.out.println(2);
        } catch(ArithmeticException ae) {
            System.out.println("除数不能为0");
        }

        System.out.println(3);
    }

}

多异常处理

A:对代码进行异常检测,并对检测的异常传递给catch处理。对每种异常信息进行不同的捕获处理。

void show(){ //不用throws 
    try{
        throw new Exception();//产生异常,直接捕获处理
    }catch(XxxException e){
//处理方式  
    }catch(YyyException e){
//处理方式  
    }catch(ZzzException e){
//处理方式  
    }
}

注意:这种异常处理方式,要求多个catch中的异常不能相同,并且若catch中的多个异常之间有子父类异常的关系,那么子类异常要求在上面的catch处理,父类异常在下面的catch处理。

public class ExceptionDemo3 {
    public static void main(String[] args) {
        try {
            String s = null;
            System.out.println(s.length());

            //int[] arr = new int[5];
            //System.out.println(arr[8]);

            //System.out.println(2 / 0);

        } 

        catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("出现数组越界了");
        } 
        catch(NullPointerException e) {
            System.out.println("出现空指针了");
        }
        catch(Exception e) {
            System.out.println("出现异常了");
        }
        /*try {
        } catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("出现数组越界了");
        }*/


    }

    private static void method() {
        try {
            String s = null;
            System.out.println(s.length());
        } catch(NullPointerException e) {
            System.out.println("出现空指针了");
        }

        try {
            int[] arr = new int[5];
            System.out.println(arr[8]);
        } catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("出现数组越界了");
        }
    }

}

Throwable常用方法&自定义异常

Throwable常用方法

String getMessage() 返回此 throwable 的详细消息字符串
String toString() 返回此 throwable 的简短描述
void printStackTrace() 打印异常的堆栈的跟踪信息

public class ExceptionDemo4 {
    public static void main(String[] args) {


        try {
            System.out.println(2 / 0);
        } catch (ArithmeticException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void method() {
        try {
            System.out.println(2 / 0);
        } catch(ArithmeticException e) {
            //String getMessage() : 原因
            //System.out.println(e.getMessage());

            //String toString()  类型和原因
            //System.out.println(e.toString());

            //void printStackTrace():类型原因和位置
            e.printStackTrace();
        }
        //System.out.println("hello");
    }
}

finally的概述和应用场景

finally使用格式:
try{
}catch(异常类型 异常变量){
}finally{
 //释放资源的代码
}
import java.io.FileWriter;
import java.io.IOException;

public class ExceptionDemo5 {
    public static void main(String[] args) {
        //method();

        FileWriter fw = null;
        try {
            System.out.println(2 / 0);
            fw = new FileWriter("a.txt");
            fw.write("hello");
            fw.write("world");
            //System.out.println(2 / 0);
            fw.write("java");

            //fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            //释放资源
            try {
                if(fw != null) {
                    fw.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    private static void method() {
        try {
            System.out.println(2 / 1);

        } catch(ArithmeticException e) {
            System.out.println("除数不能为0");
        } finally {

            System.out.println("清理垃圾");
        }
    }
}

编译时异常&运行时异常

A: 编译时期异常:是Exception的子类,非RuntimeExcpetion的子类,在编译时期必须处理
B:RuntimeException和他的所有子类异常,都属于运行时期异常。NullPointerException,ArrayIndexOutOfBoundsException等都属于运行时期异常.
运行时期异常的特点:
方法中抛出运行时期异常,方法定义中无需throws声明,调用者也无需处理此异常
运行时期异常一旦发生,需要程序人员修改源代码.

import java.io.FileWriter;
import java.io.IOException;

public class ExceptionDemo6 {
    public static void main(String[] args) {
        //System.out.println(2 / 0);

        //String s = null;
        //System.out.println(s.length());

        try {
            FileWriter fw = new FileWriter("a.txt");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值