Java关于异常的一些知识

Java关于异常的一些知识

哎,最近为了应付考试,不得不补一下java知识点了!虽然我很喜欢python,但是没办法啊!为了绩点加油,奥里给!

其实每种语言都有异常机制,那下面我就简单介绍一下:

在java中异常其实分为Exception类(运行时编程者犯的语法错误)和Error类(多半和编程者不关,也就不过多介绍)

他们共同的父类是Throwable类

这篇文章仅仅介绍一些Exception类!

Exception类有个子类为RuntimeException类(运行时异常类)比较常用!

而RuntimeException类又有如下子类:

ArrayIndexOutOfBoundsException数组下标越界
NullPointerException空指针异常
ArithmeticException算术异常
MissingResourceException丢失资源
ClassNotFoundException找不到类

如果上面没有看懂也没关系,只是作为了解,只要你会如何用就行了!

下面先给出一段代码来看看:

public class Test1 {
    public int fun(){
        int []datas = new int[10];
        try{
            datas[11] = 2;
            System.out.println("输出!");
        }
        catch (ArrayIndexOutOfBoundsException g){   //数字下标越界异常
            System.out.println("数组越界!");
        }
        catch (Exception g){                    
            System.out.println("异常");
        }
        finally {
            System.out.println("看finally语句无论是否异常一定会输出!");
        }
    }
    public static void main(String[] args) {
        Test1 test = new Test1();
        System.out.println(test.fun());
    }
}
//输出为:
数组越界!finally语句无论是否异常一定会输出!

catch语句只会执行一个

finally语句无论是否发生异常都会执行!

如果你不知道会发生什么类型的异常就可以使用Exception类(他包含所有上述异常类型!)

throw语句 和throws语句

throw语句相当于主动抛出异常,而throws语句相当于自己不处理异常而是把异常抛给调用者!

class Math{
    public int div(int i,int j) throws Exception{
        int t=i/j;
        return t;
    }
}
public class ThrowsDemo {
    public static void main(String args[]) throws Exception{
        Math m=new Math();
   }
}
public class TestThrow
{
    public static void main(String[] args) 
    {
        try
        {
            //调用带throws声明的方法,必须显式捕获该异常
            //否则,必须在main方法中再次声明抛出
            throwChecked(-3);            
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        //调用抛出Runtime异常的方法既可以显式捕获该异常,
        //也可不理会该异常
        throwRuntime(3);
    }
    public static void throwChecked(int a)throws Exception
    {
        if (a > 0)
        {
            //自行抛出Exception异常
            //该代码必须处于try块里,或处于带throws声明的方法中
            throw new Exception("a的值大于0,不符合要求");
        }
    }
    public static void throwRuntime(int a)
    {
        if (a > 0)
        {
            //自行抛出RuntimeException异常,既可以显式捕获该异常
            //也可完全不理会该异常,把该异常交给该方法调用者处理
            throw new RuntimeException("a的值大于0,不符合要求");
        }
    }
}


Java中异常的调用顺序
public class Test1 {
    public int fun(){
        int index=10;
        int []datas=new int[10];
        try{
            datas[11]=2;
            System.out.println("输出!");
            return index;
        }
        catch (ArrayIndexOutOfBoundsException g){
            System.out.println("数组越界!");
        }
        catch (Exception g){
            System.out.println("异常");
        }
        finally {
            System.out.println("看finally语句无论是否异常一定会输出!");
        }
        System.out.println("看try,catch语句之外的语句是否会输出");
        return 1;
    }

    public static void main(String[] args) {
        Test1 test=new Test1();
        System.out.println(test.fun());
    }

}
//输出结果为
数组越界!finally语句无论是否异常一定会输出!
看try,catch语句之外的语句是否会输出
1

你会发现try语句中如果发生异常,try语句中异常下面的语句就不会执行了!

然后再执行finally语句!

如果我再改下代码:

把上述代码try语句中的 data[11]=2; 删除(即让try语句不发生异常!)

结果为:

输出!
看finally语句无论是否异常一定会输出!
10

你会发现finally语句在return语句前就执行了!

所以这个顺序你应该了解了一下!

希望能够帮助你们,我曾经也是看了几篇博客才会了点,就是分享一些经验给大家!😊😊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只小猪~~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值