在Java中,使用异常专门创建自己的异常如何正确使用try、catch、finally命令?
我的任务是创建一个年龄程序,当用户输入姓名和年龄时,程序检查年龄是否在0到125之间。如果不是,程序将显示错误代码(使用异常类)。 我的代码:在Java中,使用异常专门创建自己的异常如何正确使用try、catch、finally命令?,java,exception,Java,Exception,我的任务是创建一个年龄程序,当用户输入姓名和年龄时,程序检查年龄是否在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"); }
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")
}
所以我的问题是如何正确使用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”);
}
}
}
当年龄检查失败时,您可以尝试抛出自定义异常。这将导致在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();
}
if(x首先,您需要修复语法错误
catch
和finally
应该跟在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")
}
试试看{
int x,y;
Scanner userInputScanner=新扫描仪(System.in);
System.out.println(“你叫什么名字?”);
字符串userInputName=userInputScanner.nextLine();
System.out.println(“很高兴认识你”+userInputName+“!”;
System.out.println(“你多大了?”);
扫描仪输入=新扫描仪(系统输入);
x=in.nextInt();
y=125;
if(x
了解如何使用异常。我认为Oracle的文档是一个很好的地方。请参阅。首先,您有一些语法错误。
尝试
,捕获
和最后
应该互相跟随。另外,捕获中应该是System.out.println
,而不是System.println
现在,对于异常,您不需要为异常创建第二个类,只需在catch
的括号中使用异常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");
}
}
}
公共类3岁{
公共静态void main(字符串arg[]){
试一试{
int x,y;
Scanner userInputScanner=新扫描仪(System.in);
System.out.println(“你叫什么名字?”);
字符串userInputName=userInputScanner.nextLine();
System.out.println(“很高兴认识你”+userInputName+“!”;
System.out.println(“你多大了?”);
扫描仪输入=新扫描仪(系统输入);
x=in.nextInt();
y=125;
如果(xMind)要格式化你的代码,请。我看到在catch块开始之前有多个右大括号。请确保你的catch块在try结束后出现。另外,你输入了“System.println”而不是“System.out.println”“内部捕获”。@KeqiangLi当我看到与你相同的错误时,这并不意味着我在复制你,每一个优秀的程序员都会注意到这些错误。非常感谢你。它起了作用,现在我明白了捕获最终是如何起作用的。但是使用throw的目的是什么?@Reshun:我认为你的回答在这里没有提到任何新的东西。看起来是这样的你几乎复制了另一个答案的所有内容。@ShashankKadne哇,因为我花了一些时间打开Eclipse并测试了一些东西,而且比我复制他的意思晚了一点?我不明白。如果你已经从另一个用户那里看到了答案,那么提到同样的东西有什么意义?我相信现在你将开始回答其他问题以前也回答过吗?如果你在回答的时候提出不同的观点,我会理解的!
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");
}
}
}