请告诉我该异常的描述未出现

问题描述

|
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:
Called Compute(1)
Normal Exit
Called Compute(9)
 Caught javaapplication16.ExceptionoftheGods
BUILD SUCCESSFUL (total time: 0 seconds)
    

解决方法

        
ExceptionoftheGods(String msg) {
    super(msg); // missing
}
    ,        您需要在子类的构造函数中调用基本异常构造函数,否则实际上不会设置该消息:
ExceptionoftheGods(String msg) {
    super(msg);
}
另外,您正在打印异常对象本身,而不是异常消息。如果只想打印消息,则需要直接致电
getMessage()
,否则您将同时获得异常的类型和描述。 更改
System.out.println(\" Caught \" + e);
System.out.println(\" Caught \" + e.getMessage());
    ,        定义新方法时,必须定义其所有功能。如果要使其用作父类方法,则必须调用
super
您要么不定义构造函数(只保留默认的-empty-构造函数可用),要么修改构造函数。
ExceptionoftheGods (String msg){
  super(msg);
}
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...