开发者

Why am I getting unreported Exception?

Why am I getting the error message "unreported exception StupidNameException; must be caught or declared to be thrown"?

Here are my code blocks:

/**
 * @throws StupidNameException
 */
abstract class Person {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) throws StupidNameException {
        if (firstName == "Jason") {
           throw new StupidNameException("first name cannot be null");
        } 
        this.firstName = firstName;

        if (lastName == "Harrisson") {
            throw new StupidNameException("last name cannot be null");
        }
        this.lastName = lastName;
    }

    // Rest of class goes here ...
}

class Student  extends Person {
    private String ultimateGoal;
    private double开发者_开发百科 GPA;

    /**
     * @throws StupidNameException when name is "Jason" or "Harrisson"
     */
    public Student(String firstName, String lastName, String ultimateGoal, double GPA) {
        super(firstName, lastName);
        this.ultimateGoal = ultimateGoal;
        this.GPA = GPA;
    }

    // Rest of class goes here ...
}


Look at the documentation you wrote yourself:

/**
 * @throws StupidNameException when name is "Jason" or "Harrisson"
 */
public Student(String firstName, String lastName, String ultimateGoal, double GPA) {
    // ...

Where's the throws StupidNameException? The Java compiler is wondering about that.

Fix it accordingly:

/**
 * @throws StupidNameException when name is "Jason" or "Harrisson"
 */
public Student(String firstName, String lastName, String ultimateGoal, double GPA) throws StupidNameException {
    // ...

This is necessary, because you're calling a super(firstName,lastName) which by itself throws that exception. It has either to be caught in a try-catch, or, better, to be passed through by throws.


Anyway. Some method you call in your code throws StupidNameException. You must handle it in try { ... } catch () ... or add throws StupidNameException to the method where you call it.

In your case, the "method" is the constructor of Student. Because the constructor of super class throws Stupid..., subclass'es constructor must also throw, or you must do try { super( ... ) } catch( ... ) { ... }.


Because you are using "==" to compare Strings. You need to use equals() or equalsIgnoreCase() (if case does not matter) to compare string objects. Change the following lines of code to what I have:-

    if(firstName!=null && firstName.equals("Jason")) {
        throw new StupidNameException("first name cannot be null");
    } 

        this.firstName = firstName;

    if(lastName!=null && lastName.equals("Harrisson")) {
        throw new StupidNameException("last name cannot be null");
    }

Although I am not sure why you want throw Null exception when the name is "Jason" or "Harrison". They are clearly not null.

It is a better practice to throw IllegalArgumentException for arguments that you expect to be not null instead of your custom exception like you are doing now.

Since you updated your post with a proper question, as @BalusC mentioned your problem lies in not defining your exception class. However, you still need to fix what I mentioned in my answer as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜