定制异常

时间:2012-06-14 05:33:41

标签: java exception

我做了一个自定义异常,但我不确定它是否正确,而且我不知道为什么是一个异常情况,程序不是为异常打印消息而只是为了类的名称自定义异常,以及如何在异常情况下终止程序,因为目前程序只会通知异常并继续。该程序正在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 个答案:

答案 0 :(得分: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);
    }

注意&&而不是||

答案 1 :(得分:2)

在您的Exception类中重写toString()方法

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

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

答案 2 :(得分:1)

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

打印例外的详细信息

使用

e.printStackTrace()

而不是System.out.println(e);


另见

答案 3 :(得分: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);
    }