这是我的当前代码:
import java.lang.String;
import java.io.*;
class InvalidAgeException extends Exception
{
public InvalidAgeException()
{
super("The age you entered is not between 0 and 125");
}
}
public class questionOne
{
public static void main(String args[])
{
System.out.println("What is your name?");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name;
try
{
name = br.readLine();
}
catch(IOException e)
{
System.out.println("Error: " + e);
System.exit(1);
}
System.out.println("Hello " + name + ", how old are you?");
String i;
int age;
try
{
i = br.readLine();
age = Integer.valueOf(i);
}
catch(IOException e)
{
System.out.println("Error: " + e);
System.exit(1);
}
catch(InvalidAgeException e)
{
System.out.println("Error: " + e);
System.exit(1);
}
finally
{
System.out.println("No errors found.");
}
}
}
这个任务是编写一个程序,要求用户输入姓名和年龄,如果年龄不在0到125之间,则抛出异常。我的代码出现了两个错误:
questionOne.java:31: variable name might not have been initialized
System.out.println("Hello " + name + ", how old are you?");
^
questionOne.java:46: exception InvalidAgeException is never thrown in body of corresponding try statement
catch(InvalidAgeException e)
^
2 errors
我不确定如何修复它们。