如何清除Java代码中的未处理异常错误

时间:2020-09-30 14:39:38

标签: java exception

我是Java的新手,正在尝试处理异常处理代码。在我收到未处理的异常错误之前,一切对我来说都很好。谁能帮我纠正代码并告诉我错误,使我再也无法提交?

异常类-创建该类以检索不同异常的消息

 // Implement user defined exception classes 
class InvalidAgeException extends Exception{

public InvalidAgeException(String message) {
    super(message);
}

}
class InvalidJobProfileException extends Exception{

public InvalidJobProfileException(String message) {
    super(message);
}

}
class InvalidNameException extends Exception{

public InvalidNameException(String message) {
    super(message);
}

}

申请人类别-用于设置和获取申请人属性的类别

class Applicant {

private String name;
private String jobProfile;
private int age;

public String getName() {
    return name;
}
  
public void setName(String name) {
    this.name = name;
}
  
public String getJobProfile() {
     return jobProfile;
}
  
public void setJobProfile(String jobProfile) {
    this.jobProfile = jobProfile;
}
  
public int getAge() {
    return age;
}
  
public void setAge(int age) {
    this.age = age;
}
}

验证者类-用于检查申请人是否有姓名的类

class Validator{
    //Implement your code here
            public boolean validateName(String name) throws Exception
            {
            if(getName().length()>0)
             {
            return true;
             }
            else{
            return false;
        }

}
    public boolean validateJobProfile(String jobProfile) throws Exception
   {
    if (getJobProfile().equalsIgnoreCase("Associate") || getJobProfile().equalsIgnoreCase("Clerk") || 
    getJobProfile().equalsIgnoreCase("Executive") || getJobProfile().equalsIgnoreCase("Officer"))
    {
        return true;
    }
    else{
        return false;
    }
}
public boolean validateAge(int age) throws Exception
{
    if(getAge()>=18 && getAge()<=30)
    {
        return true;
    }
    else{
        return false;
    }
}
public void validate(Applicant applicant) throws Exception
{
    if(validateName(getName())==false)
    {
        throw new InvalidNameException("Invalid Name");
    }
    if (validateJobProfile(getJobProfile())==false)
    {
        throw new InvalidJobProfileException("Invalid job post");
    }
    if (validateAge(getAge())==false)
    {
        throw new InvalidAgeException("Invalid Age");
    }

}
}

测试器类-主类,用于创建不同类的对象

class Tester {
public static void main(String[] args) {
        
    try {
        Applicant applicant= new Applicant();
        applicant.setName("Jenny");
        applicant.setJobProfile("Clerk");
        applicant.setAge(25);
        
        Validator validator = new Validator();
              
        validator.validate(applicant);
        System.out.println("Application submitted successfully!");
    } 
    catch (InvalidNameException|InvalidJobProfileException|InvalidAgeException e) {
        System.out.println(e.getMessage());
    }
}
}

2 个答案:

答案 0 :(得分:0)

您的方法声明为throws Exception。因此,您实际上必须抓住Exception。如果您只想捕获这三个自定义异常中的任何一个,则需要声明您的方法仅通过throws InvalidNameException, InvalidJobProfileException, InvalidAgeException

抛出这三个异常

此外,您的validateAge被声明为引发异常,但实际上从未引发任何异常。

答案 1 :(得分:0)

您的方法需要指定它们实际抛出的异常。目前,您只是在简单地写道,他们抛出了一般的Exception,因此您不会陷入其中。
更改

public void validate(Applicant applicant) throws Exception{...}

public void validate(Applicant applicant) throws InvalidNameException, InvalidJobProfileException, InvalidAgeException{...}

对于其他方法,您需要进行类似的操作。

相关问题