简体   繁体   English

类Exception的默认构造函数?

[英]Default constructor of class Exception?

package com.psl.exception;

public class NoDataFoundException extends Exception {

    public NoDataFoundException(){
        super("No Data Found");
    }

    public NoDataFoundException(String msg){
        super(msg);
    }
}

What does the class Exception do with the string that is passed? Exception类对传递的字符串有何作用? I tried it and it does nothing. 我试过了,它什么也没做。 What is the point of passing the string to the constructor of Exception then? 然后将字符串传递给Exception的构造函数有什么意义?

它在Throwable设置作为消息传递的String ,这通常是有用的自定义消息,当引发异常时会打印该消息,也可以通过Throwable上的getMessage()方法进行检索

The documentation says: 文件说:

Constructs a new exception with the specified detail message. 使用指定的详细消息构造一个新的异常。

The detail message is available from getMessage and usually included in toString and such. 详细消息可从getMessage ,通常包含在toString等中。

One of the constructors of Exception takes a string to use as the detail message. Exception的构造函数之一采用字符串作为详细信息。

See here: http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html 参见此处: http : //docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

The default constructor uses a null detail message, but other components such as a message and throwable can be specified 默认的构造函数使用详细信息为空,但是可以指定其他组件,例如消息和throwable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM