-3

首先,我只想说,我知道这个程序可以通过使用 if-else 配置或 while 循环更容易地完成,但我想知道如何使用异常类来完成它。(我猜我需要使用 try-catch 块的自定义异常)。


import java.util.Scanner;

class AgeChecker
{
    public static void main(String [] args)
    {
        Scanner inputdata = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = inputdata.nextLine();
        System.out.print(name+", enter your age: ");
        int age = inputdata.nextInt();
        try
        { // age entered must be between 0-125 (how to trigger exception in catch block?)
            System.out.println("You entered: "+age);
        }
        catch(Exception e)
        {
            System.out.println("Out of range error! (must be between ages 0 and 125)"+e);
        }
        finally
        {
            System.out.println("Age Checking Complete.");
        }
    }
}
4

4 回答 4

1

虽然我不确定为什么不使用 if-else 而不是异常,但您可以通过继承现有异常类来制作自定义异常。通常,您将 Exception 子类化为“声明的”异常,并将 RuntimeException 子类化为您不想捕获的异常。在您的情况下,您可以只继承 Exception,例如:

public MyException extends Exception
{
}

如果发现范围问题,则将其抛出:

throw new MyException();

然后通过捕获异常来捕获它,就像你做的那样或 MyException:

catch(MyException exp) ...
于 2013-08-03T05:19:06.940 回答
1
package example.stackoverflow;

import java.util.Scanner;

public class AgeChecker
{
    public static final int MIN_AGE = 0;
    public static final int MAX_AGE = 125;

    static class InvalidAgeException extends Exception
    {
        private static final long serialVersionUID = 1340715735048104509L;

        public InvalidAgeException()
        { }

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

        public InvalidAgeException(String message, Throwable cause)
        {
            super(message, cause);
        }
    }

    public static void main(String[] args)
    {
        Scanner inputdata = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = inputdata.nextLine();
        System.out.print(name+", enter your age: ");
        int age = inputdata.nextInt();
        try
        {
            System.out.println("You entered: "+age);
            // Assuming age limits are non-inclusive
            if( (age <= MIN_AGE) || (age >= MAX_AGE) )
            {
                throw new InvalidAgeException("Out of range error! (must be between ages 0 and 125)");
            }
        }
        catch(InvalidAgeException e)
        {
            e.printStackTrace();
        }
        finally
        {
            System.out.println("Age Checking Complete.");
            if(inputdata != null)
            {
                inputdata.close();
            }
        }
    }
}

另请注意,当您完成扫描仪时,您需要关闭()您的扫描仪。在 finally 块中进行此类清理通常很有用,因此我在此示例中将其放在了此处。

于 2013-08-03T05:23:12.620 回答
0

您可以创建自己的Exception. 如果你想触发一个catch块,你需要throwException你的try

try {
    System.out.println("Enter your age: ");
    int age = scanner.nextInt();
    if(isInvalid(age)) {
        throw new Exception("Age is invalid!");
    }
} catch (Exception e) {
    // etc
}

您可能还想考虑子类Exception化以提供有关导致问题的原因的更多信息。

于 2013-08-03T05:24:15.873 回答
0
public class yourExcetionActivity extends Exception 
{
    public yourExcetionActivity() { }
    public yourExcetionActivity(String string) {
        super(string);
    }
}
于 2013-08-03T05:32:29.163 回答