Java异常的捕获与处理

Java提供了try(尝试)catch(捕捉)finally(最终)这三个关键字来处理异常。
在处理各种异常时,需要用到对应的异常类,指的是由程序抛出的对象所属的类。

一、异常处理的使用

由于finally块是可以省略的,异常处理格式可以分为三类:

try{ }——catch{ }

try{ }——catch{ }——finally{ }

try{ }——finally{ }

public class DealException
{
    public static void main(String args[])
    {
        try
        //要检查的程序语句
        {
            int a[] = new int[5];
            a[10] = 7;//出现异常
        }
        catch(ArrayIndexOutOfBoundsException ex)
        //异常发生时的处理语句
        {
            System.out.println("超出数组范围!");
        }
        finally
        //这个代码块一定会被执行
        {
            System.out.println("*****");
        }
        System.out.println("异常处理结束!");
    }
}

可以看出,在异常捕捉的过程中要进行两个判断,第一是try程序块是否有异常产生,第二是产生的异常是否和catch()括号内想要捕捉的异常相同。

那么,如果出现的异常和catch()内想要捕捉的异常不相同时怎么办呢?事实上我们可以在一个try语句后跟上多个异常处理catch语句,来处理多种不同类型的异常。

public class DealException
{
    public static void main(String args[])
    {
        try
        //要检查的程序语句
        {
            int a[] = new int[5];
            a[0] = 3;
            a[1] = 1;
            //a[1] = 0;//除数为0异常
            //a[10] = 7;//数组下标越界异常
            int result = a[0]/a[1];
            System.out.println(result);
        }
        catch(ArrayIndexOutOfBoundsException ex)
        //异常发生时的处理语句
        {
            System.out.println("数组越界异常");
            ex.printStackTrace();//显示异常的堆栈跟踪信息
        }
        catch(ArithmeticException ex)
        {
            System.out.println("算术运算异常");
            ex.printStackTrace();
        }
        finally
        //这个代码块一定会被执行
        {
            System.out.println("finally语句不论是否有异常都会被执行。");
        }
        System.out.println("异常处理结束!");
    }
}

上述例子中ex.printStackTrace();就是对异常类对象ex的使用,输出了详细的异常堆栈跟踪信息,包括异常的类型,异常发生在哪个包、哪个类、哪个方法以及异常发生的行号。

二、throws关键字

throws声明的方法表示该方法不处理异常,而由系统自动将所捕获的异常信息抛给上级调用方法。

public class throwsDemo
{
    public static void main(String[] args)
    {
        int[] a = new int[5];
        try
        {
            setZero(a,10);
        }
        catch(ArrayIndexOutOfBoundsException ex)
        {
            System.out.println("数组越界错误!");
            System.out.println("异常:"+ex);
        }
        System.out.println("main()方法结束。");
    }
    private static void setZero(int[] a,int index) throws ArrayIndexOutOfBoundsException
    {
        a[index] = 0;
    }
}

throws关键字抛出异常,“ArrayIndexOutOfBoundsException”表明setZero()方法可能存在的异常类型,一旦方法出现异常,setZero()方法自己并不处理,而是将异常提交给它的上级调用者main()方法。

三、throw关键字

throw的作用是手工抛出异常类的实例化对象。

public class throwDemo
{
    public static void main(String[] args)
    {
        try
        {
            //抛出异常的实例化对象
            throw new ArrayIndexOutOfBoundsException("\n个性化异常信息:\n数组下标越界");
        }
        catch(ArrayIndexOutOfBoundsException ex)
        {
            System.out.println(ex);
        }
    }
}

我们能发现,throw好像属于没事找事,引发运行期异常,并自定义提示信息。事实上,throw通常和throws联合使用,抛出的是程序中已经产生的异常类实例。

ExceptionDemo

输出结果:
setZero方法开始:
setZero方法结束。
异常:java.lang.ArrayIndexOutOfBoundsException: 10
main()方法结束!

四、RuntimeException类

Exception和RuntimeException的区别:

Exception:强制性要求用户必须处理;
RunException:是Exception的子类,由用户选择是否进行处理。

在这里插入图片描述

五、自定义异常类

自定义异常类继承自Exception类,可以使用父类的大量的方法,也可自己编写方法来处理特定的事件。Java提供用继承的方式运行用户自己编写的异常类。

class MyException extends Exception
{
    public MyException(String message)
    {
        super(message);
    }
}
public class DefinedException
{
    public static void main(String[] args)
    {
        try
        {
            throw new MyException("\n自定义异常类!");
        }
        catch(MyException e)
        {
            System.out.println(e);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43520450/article/details/106867527