1

我做了一个自定义异常,但我不确定它是否正确,而且我不知道为什么会出现异常,程序没有打印异常消息,而只打印自定义异常的类的名称,以及如何在出现异常的情况下终止程序,因为此时程序只通知异常并继续。该程序正在eclipse上编译和运行。干杯,伙计们。这是代码。

import java.util.Scanner;
public class Stud
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter age of the student");
        int age = input.nextInt();
        try
        {
                if(age < 7 || age > 18)
                {   
                    throw new InvalidStudentException("Age can't be less than 7 and more than 18");
                }
        }
        catch(InvalidStudentException e)
        {
                System.err.println(e) ;
        }

        System.out.println("Enter the gender of the student");
        String gender = input.next();
        try
        {
            if((!gender.equals("male")) || (!gender.equals("female")))
            {
                throw new InvalidStudentException("Gender can be only male or female");
            }
        }catch(InvalidStudentException e)
        {
            System.err.println(e) ;
        }

        System.out.println("Enter the name of the student");
        String name = input.next();
        try
        {


            if(name.length() < 10 || name.length() > 50)
            {
                throw new InvalidStudentException("The length of the name must be greater than 10 and less than 50 characters");
            }
        }catch(InvalidStudentException e)
        {
            System.err.println(e) ;
        }

        System.out.println("Enter the class number for the student");
        int classNumber = input.nextInt();

        try
        {
            if(classNumber < 1 || classNumber > 11)
            {
                throw new InvalidStudentException("Class number can be only between 1 and 11");
            }
        }catch(InvalidStudentException e)
        {
            System.err.println(e) ;
        }
    }
}

/** the class with the custom made exception
 */
public class InvalidStudentException extends Exception
{
    String msg;
    int num;

     public InvalidStudentException() 
     {
         super();
     }

     public InvalidStudentException(String msg) 
     {
         super();
         this.msg = msg;
     }
     public InvalidStudentException(int num) 
     {
         super();
         this.num = num;
     }
}
4

4 回答 4

2

如果性别不是男性也不是女性,则需要引发异常,因此将代码更改为:

try
    {
        if((!gender.equals("male")) && (!gender.equals("female")))
        {
            throw new InvalidStudentException("Gender can be only male or female");
        }
    }catch(InvalidStudentException e)
    {
        System.err.println(e.getMessage()) ;
        System.exit(0);
    }

注意&&而不是||

于 2012-06-14T05:49:54.790 回答
2

在您的 Exception 类中覆盖 toString() 方法

@Override
 public String toString()
 {
     return this.msg.toString();
 }

打印堆栈跟踪是另一个问题。

于 2012-06-14T05:53:07.037 回答
1

您正在捕获异常,因此程序也会在异常情况下继续运行。您可以throw从 catch 块中重新使用它或使用System.exit()

打印异常的详细信息

利用

e.printStackTrace()

代替System.out.println(e);


另见

于 2012-06-14T05:36:08.183 回答
1

尝试这个

    try
    {
        if((!gender.equals("male")) || (!gender.equals("female")))
        {
            throw new InvalidStudentException("Gender can be only male or female");
        }
    }catch(InvalidStudentException e)
    {
        System.err.println(e.getMessage()) ;
        System.exit(0);
    }
于 2012-06-14T05:38:58.040 回答