去看看Throwable Exception Error RuntimeException 的继承关系就明白了

解决方案 »

  1.   

    自定义异常若继承RuntimeException如(这里把RuntimeException看做继承RuntimeException的一个自定义异常类):
    public static void main(String[] args) {
    ClassTest2 t = new ClassTest2();
    t.eat();
    }
    public void eat()throws RuntimeException{

    }这里使用者(main函数)不用对这一异常进行try捕捉或throws继续往外抛,这样编译器不会报错是能运行的,在运行的时候符合自定义异常条件就自动转向自定义异常类。
    自定义异常若继承Exception如(这里把Exception看做继承Exception的一个自定义异常类):public static void main(String[] args) throws Exception {
    ClassTest2 t = new ClassTest2();
    t.eat();
    }
    public void eat()throws Exception{

    }这里使用者(main函数)必须对这一异常进行try捕捉或throws继续往外抛,要不然编译器在没运行时就会报错,如图:。
      

  2.   

    上面第二块的代码:<span style="color: #FF0000;"> throws Exception</span>是 throws Exception所以:
    我们自定义异常一般都直接继承Exception。除非你写的自定义异常不想处理,又想让编译器在运行之前不报错,可以继承RuntimeException(RuntimeException毕竟是Exception的子类)