繁体   English   中英

在 Java 中使用 Exceptions 专门创建自己的异常如何正确使用 try、catch、finally 命令?

[英]In Java using Exceptions specifically creating own exception how do I properly use the try, catch, finally command?

我的任务是创建一个年龄程序,当用户输入他们的姓名和年龄时,程序会检查年龄是否在 0 到 125 之间。如果不是,程序会显示错误代码(使用异常类)。 我的代码:

import java.util.Scanner;
import java.io.IOException;
class InvalidAgeException extends Exception
{
public InvalidAgeException()
{
super("The Age you've entered is not valid");
}
}
class age3
{
public static void main(String arg[])
{
try
{
int x, y;
Scanner userInputScanner = new Scanner(System.in);
System.out.println("What is your name?");
String userInputName = userInputScanner.nextLine();
System.out.println("Nice to meet you " + userInputName +"!");
System.out.println("How old are you?");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = 125;
if (x<y)
System.out.println("Correct.Your age is less than 125");
else
System.out.println("Not Possible");
}
}
}
catch(InvalidAgeException e)
{
System.println("Invalid age error"+ e);
}
finally
{
System.out.println("Execution Completed")
}

基本上我做了我的声明,如果错误,它应该显示一条消息,说他们错了,最后应该有一个例外,表明这个人犯了错误。

这些是我得到的错误:

3 错误

Mishas-MBP:desktop mishashtapov$ javac age3.java
age3.java:14: error: 'try' without 'catch', 'finally' or resource    declarations
try
^
age3.java:32: error: class, interface, or enum expected
catch(InvalidAgeException e)
^
age3.java:35: error: class, interface, or enum expected
}
^
3 errors

所以我的问题是如何正确使用 try、catch、finally 方法以便它显示异常??? 任何帮助,将不胜感激。 谢谢。

我使用了在线课程中的这个大纲:

异常处理 创建用户定义的异常 在 Java 中,可以创建自己的异常而不是使用预定义的异常。 创建您自己的异常就是创建一个用户定义的异常。 现在看看基于年龄计算创建的异常。 当用户在程序中输入他们的年龄时,系统会要求您检查他们的年龄。 如您所知,一个人的年龄范围是有限的:比如 0(出生)到 125。在编写程序时,您指定这个范围,这样如果用户给出的数字超出这个范围,程序可以显示一个给用户的错误信息。 在这种情况下,您必须创建自己的异常。 查看以下代码中定义新异常创建的步骤。

Class InvalidAgeException extends Exception
{
public InvalidAgeException()
{
super("The Age you've entered is not valid");
}
}
class TestException
{
public static void main(String arg[])
{
try
{
     //Your Code Here
}
catch(InvalidAgeException ae)
{
System.println(“Your Exception”);
}
}
}

首先,您需要修复语法错误。

catchfinally应该遵循 try 块的右花括号。 此外,它应该是System.out.println而不是System.println 而且,您的异常应该在您的逻辑中创建和抛出。

你的代码应该是

try {
     int x, y;
     Scanner userInputScanner = new Scanner(System.in);
     System.out.println("What is your name?");
     String userInputName = userInputScanner.nextLine();
     System.out.println("Nice to meet you " + userInputName + "!");
     System.out.println("How old are you?");
     Scanner in = new Scanner(System.in);
     x = in.nextInt();
     y = 125;
     if (x < y)
         System.out.println("Correct.Your age is less than 125");
     else {
         throw new InvalidAgeException("Not Possible");
     }
} catch (InvalidAgeException e) {
    System.out.println("Invalid age error" + e);
} finally {
    System.out.println("Execution Completed")
}

关于如何使用异常。 我认为 Oracle 的文档将是一个查看的好地方。 请参阅例外

当您的年龄检查失败时,您可以尝试抛出您的自定义异常。 这将导致在 catch 子句中捕获异常并打印错误。 就像是 :

    if (x<y)
System.out.println("Correct.Your age is less than 125");
else {
System.out.println("Not Possible");
//throw your exception here:
throw new InvalidAgeException();
}

首先,你有一些语法错误。 trycatchfinally应该互相跟随。 此外,它应该是System.out.println而不是System.println在您的捕获中。

现在是异常,您的异常不需要第二个类,只需在catch的括号之间使用Exception e

以下对我有用:

public class age3{

    public static void main(String arg[]){

        try{
            int x, y;
            Scanner userInputScanner = new Scanner(System.in);
            System.out.println("What is your name?");
            String userInputName = userInputScanner.nextLine();
            System.out.println("Nice to meet you " + userInputName +"!");
            System.out.println("How old are you?");
            Scanner in = new Scanner(System.in);
            x = in.nextInt();
            y = 125;
            if (x<y)
            System.out.println("Correct.Your age is less than 125");
            else
            System.out.println("Not Possible");
        }catch(Exception e){
            System.out.println("Invalid age error" + e);
        }finally{
            System.out.println("Execution Completed");
        }

    }
}

PS:下次尝试编码更整洁一点,这样自己和他人都更容易阅读:P

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM