throw new RuntimeException为什么不需要再方法体上加throws 关键字 抛出这个异常

不知道有没有仁兄碰到了和我一样的这个疑惑,为什么在方法体的内部使用 throw newRuntimeException 不需要加上 throws 也不会出现一异常

① 原因是RuntimeException 是被程序识别为程序员的编写错误,无需对外抛出,类似于数组下标越界,算数异常等

② 而类似于IOException 和 ClassNotFoundExcption 则需要手动抛出

在这里插入图片描述

package Review;

/**
 * @Author:CT
 * @Date:2021/3/5
 * @Description:Review
 * @Version 1.0
 */
public class Review01 {
    
    
    public static void main(String[] args) {
    
    

    }
}
class Errors{
    
    

    void show(){
    
    

        throw new RuntimeException("运行出错");
    }

}


class Errorss{
    
    

    void show () throws Exception {
    
    
        throw new Exception();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_46351306/article/details/114408028