zoukankan      html  css  js  c++  java
  • Java异常处理-----运行时异常(RuntimeException)

    RuntimeException
    RunntimeException的子类:

    ClassCastException

    多态中,可以使用Instanceof 判断,进行规避

    ArithmeticException

    进行if判断,如果除数为0,进行return

    NullPointerException

    进行if判断,是否为null

    ArrayIndexOutOfBoundsException

    使用数组length属性,避免越界

    这些异常时可以通过程序员的良好编程习惯进行避免的

    1:遇到运行时异常无需进行处理,直接找到出现问题的代码,进行规避。
    2:就像人上火一样牙疼一样,找到原因,自行解决即可
    3:该种异常编译器不会检查程序员是否处理该异常
    4:如果是运行时异常,那么没有必要在函数上进行声明。

    6:案例

    1:除法运算功能(div(int x,int y))
    2:if判断如果除数为0,throw new ArithmeticException();
    3:函数声明throws ArithmeticException
    4:main方法调用div,不进行处理
    5:编译通过,运行正常
    6:如果除数为0,报异常,程序停止。
    7:如果是运行时异常,那么没有必要在函数上进行声明。

    1:Object类中的wait()方法,内部throw了2个异常 IllegalMonitorStateException InterruptedException


    1:只声明了一个(throws) IllegalMonitorStateException是运行是异常没有声明。

    class Demo{
    
        public static void main(String[] args){
            div(2, 1);
        }
    
        public static void div(int x, int y) {
            if (y == 0) {
                throw new ArithmeticException();  
            }
            System.out.println(x / y);
        }
    }

    【正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个“顶”字,你就顺手把它点了吧(要先登录CSDN账号哦 )】


    —–乐于分享,共同进步!
    —–更多文章请看:http://blog.csdn.net/duruiqi_fx


  • 相关阅读:
    hdu acm 2844 Coins 解题报告
    hdu 1963 Investment 解题报告
    codeforces 454B. Little Pony and Sort by Shift 解题报告
    广大暑假训练1 E题 Paid Roads(poj 3411) 解题报告
    hdu acm 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
    hdu acm 1114 Piggy-Bank 解题报告
    poj 2531 Network Saboteur 解题报告
    数据库范式
    ngnix 配置CI框架 与 CI的简单使用
    Vundle的安装
  • 原文地址:https://www.cnblogs.com/hainange/p/6153840.html
Copyright © 2011-2022 走看看