小编Abh*_*ini的帖子

在Java中,默认处理程序没有捕获我们抛出的异常?对

但是在下面的程序中,当在catch语句中重新抛出异常时,没有throws子句,没有错误吗?怎么样?

Class Throwdemo {
  static void demoproc(){
    try{
        throw new NullPoinerException ("demo");
    }catch(NullPointerException e) {
        System.out.println("Caught inside demoproc."); 
        throw e;
    }
  }
  public static void main(String Args[]){
    try[
        demoproc();
    }catch(NullPointerException e) {
            System.out.println("Recaught : " + e);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

输出是

Caught inside demoproc.
Recaught : java.lang.NullPointerException: demo
Run Code Online (Sandbox Code Playgroud)

java exception-handling

2
推荐指数
1
解决办法
150
查看次数

请告诉我,异常的描述没有出现

package javaapplication16;

class ExceptionoftheGods extends Exception {

    double b;

    ExceptionoftheGods (String msg){
    }

}

class mak {

    static void compute(int a) throws ExceptionoftheGods {
        System.out.println("Called Compute(" + a + ")");
        if(a > 7) {
            throw new ExceptionoftheGods("Dog");
        }
        System.out.println("Normal Exit");
    }    


    public static void main(String[] args) {
        try {
            compute(1);
            compute(9);
        } catch(ExceptionoftheGods e) {
            System.out.println(" Caught " + e);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

OUTPUT

run:
Called Compute(1)
Normal Exit
Called Compute(9)
 Caught javaapplication16.ExceptionoftheGods
BUILD SUCCESSFUL (total time: 0 seconds)
Run Code Online (Sandbox Code Playgroud)

java exception-handling

-1
推荐指数
1
解决办法
200
查看次数

标签 统计

exception-handling ×2

java ×2