简体   繁体   English

检查异常

[英]checked exceptions

I have been revising for a java exam. 我一直在为参加Java考试而修订。 I'm not sure how to answer the following question about checked exceptions. 我不确定如何回答以下有关检查异常的问题。 Any help is appreciated. 任何帮助表示赞赏。 Cheers 干杯

InvalidMemberIdException is a checked exception class. InvalidMemberIdException是一个已检查的异常类。 It has two constructors: one that takes no parameters and the other that takes a single String parameter that is used as the message associated with the exception when it is thrown. 它有两个构造函数:一个不带参数的构造函数,另一个带单个String参数的构造函数,该参数用作引发异常时与异常关联的消息。 Write the full definition of the InvalidMemberIdException class. 编写InvalidMemberIdException类的完整定义。

something like: 就像是:

public class InvalidMemberIdException extends Exception {    
    InvalidMemberIdException(){
       super();
    }

    InvalidMemberIdException(String message){
       super(message);
    }

}

the key thing to note is that according to 要注意的关键是

http://download.oracle.com/javase/6/docs/api/java/lang/Exception.html http://download.oracle.com/javase/6/docs/api/java/lang/Exception.html

Exception already has a message field, so I am just creating a new exception type that hooks into the Exception class's message field. 异常已经有一个消息字段,所以我只是创建一个新的异常类型,该异常类型挂接到Exception类的消息字段中。 Also, note that Exception is a checked exception, so so is this new exception type. 另外,请注意,Exception是一个已检查的异常,所以这个新的异常类型也是如此。

Your class should extend from the Exception class. 您的类应从Exception类扩展。 It should contain two constructors, one without parameters, second with one String parameter. 它应包含两个构造函数,一个不带参数,另一个带一个String参数。 Both constructors should call corresponding constructors from the superclass. 这两个构造函数都应从超类调用相应的构造函数。

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

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