【JAVASE基础】异常

这篇博客详细介绍了Java异常处理的各个方面,包括try-catch语句的使用,多catch并行处理异常,throw和throws关键字的运用,finally代码块的执行特点,以及RuntimeException和自定义异常的创建。通过实例展示了如何处理和定制异常,强调了异常处理在程序中的重要性,特别是对于资源释放和程序稳定性的作用。
摘要由CSDN通过智能技术生成

1.异常
异常的知识点不好理解,要求同学们学习异常,主要的目的记住使用格式
1.1 try…catch异常处理
try catch的异常处理的格式写法:
try{
被检测的代码
可能发生异常的代码
}catch(异常类的类名变量名){
异常的处理方式:写什么都可以
定义变量,创建对象,调用方法,循环,判断…
只要写了catch,异常就被处理掉了I
}

package com.sdjzu.exception;

public class ExceptionTest {
    public static void main(String[] args) {
        exception();
    }
    public static void exception(){
        int[] str={1,2};
        str[3]=3;
        System.out.println("str = " + str);
        System.out.println(1111);
    }
}

在这里插入图片描述

package com.sdjzu.exception;

public class ExceptionTest {
    public static void main(String[] args) {
        exception();
    }
    public static void exception(){
        int[] str={1,2};
        try{
            str[3]=3;
            System.out.println("str = " + str);
        }catch(Exception ex){
            System.out.println("异常被处理");
        }


        System.out.println(1111);
    }
}

在这里插入图片描述
打印异常信息

package com.sdjzu.exception;

public class ExceptionTest {
    public static void main(String[] args) {
        exception();
    }
    public static void exception(){
        int[] str={1,2};
        try{
            str[3]=3;
            System.out.println("str = " + str);
        }catch(Exception ex){
            System.out.println("异常被处理");
            /*
            * Throwable类的异常信息的方法
            * */
            String s1=ex.getMessage();
            System.out.println("s1 = " + s1);
            String s2=ex.toString();
            System.out.println("s2 = " + s2);
            ex.printStackTrace();
        }


        System.out.println(1111);
    }
}

在这里插入图片描述
1.2多catch并行处理
I
异常处理的代码中: try可以跟随多个catch
好处:不同的异常,可以区别对待.分开处理

package com.sdjzu.somecatch;

public class SomecatchTest {
    /*
    * 多catch异常
    * */

    public static void main(String[] args) {
        try {
            exc(1);
        }catch (NullPointerException ex){
            System.out.println("控制针异常被处理");
        }catch (ArrayIndexOutOfBoundsException ex){
            System.out.println("越界异常被处理");
        }
    }
    public static void exc(int i){
        //空指针异常
        if(i==0){
            String[] str=null;
            System.out.println("str.length = " + str.length);

        }
        //数组越界异常
        if(i==1){
            int[] str={1,2};
            str[4]=4;

        }
    }
}

在这里插入图片描述
多个catch处理异常的时候写法特别注意:如果catch中的异常类没有关系,先写
后写没有区别,catch中的异常类有继承关系.父类写在最下面
1.3 throw和throws关键字的使用
●throw关键字:只能写在方法内部,关键字的后面跟随对象的创建
●throws关键字:只能写在方法的定义上,关键字后面跟随异常类名

package com.sdjzu.throw1;

public class Exception {
    public static void main(String[] args) {
        int length=-3;
        try {
            System.out.println("getArea(length) = " + getArea(length));
        }catch (java.lang.Exception e){
            e.printStackTrace();
        }
    }
    //计算正方形的面积
    public static int  getArea(int length) throws java.lang.Exception {
        if(length<=0){
            throw new java.lang.Exception("参数不合法");
        }
        return length*length;
    }

}

在这里插入图片描述
1.4 finally代码块
finally代码块跟随try … catch使用.也有跟随try使用
finally代码块里面的程序,无论是否出现异常,都会执行,必须执行
结束JVM了,finally不执行.
主要用于释放资源

package com.sdjzu;
/*
* finally代码块
* */
public class FinallyTest {
    public static void main(String[] args) {
        method();
    }
    public static void method(){
        //异常的处理
        try{
            int[] arr={2};
            System.out.println("arr[1] = " + arr[1]);
        }catch (Exception e){
            e.printStackTrace();
        }
        //无论出不出现异常finally代码块都要运行
        finally{
            System.out.println("无论出不出现异常finally代码块都要运行");
            //不要再finally中return
            //return 1;
        }
    }
}

在这里插入图片描述
在这里插入图片描述
1.5 RuntimeException异常
异常的父类是Exception,Exception类的子类RuntimeException,凡是
RuntimeException和他的所有子类都称为运行异常,非子类的称为编译异常
●编译异常:方法出现编译异常,调用者必须处理否则编译失败.处理方式可以是try
catch或者是throws都可以
●运行异常:方法出现运行异常,方法的定义上不需要throws声明,调用者也不需要
处理这个异常
不要处理运行异常:程序- -旦发生运行异常,请程序人员修改源码
●常见的运行异常
。Nu11PointerException 空指针
。IndexoutofBoundsException 越界异常
。ClassCastException 类型强制
。Il1ega1ArgumentException 无效的参数异常
1.6自定义异常.
Java官方已经定义了大量的异常类,但是依然不够,以后做项目的时候会出现的异
常在DK中没有定义的需要我们自己定义异常
●自定义异常,入伙,继承Exceptipn或者RuntimeException
。只有Exception和他的子类才具有可抛出性
●自定义的类中构造方法super调用父类构造方法,传递异常信息

package com.sdjzu.difinedExceptionClass;
/*
* 自定义异常类
* */
public class ScoreException extends RuntimeException{
    //显示异常信息
    //自定义类的构造器进行传参,调用父类的构造器
    public ScoreException(String s){
        super(s);
    }
}

package com.sdjzu.difinedExceptionClass;

public class ExceptionTest {
    public static void main(String[] args) {
        getScore(100,-2);
    }
    //求平均值的方法
    public static int getScore(int math,int Chinese){
        //数据的安全效验
        if(math<0||Chinese<0){
            //抛出自定义异常
            throw new ScoreException("成绩输入错误");
        }
        return (math+Chinese)/2;
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值