大家好!我目前在学习JAVA的抛出异常。以下是我自己编写的程序:
import java.io.*;
class Myexception extends Exception
{
private String mistake;
public Myexception(String message,String mistake)
{
super(message);
this.mistake = mistake;
}
public String getMistake()
{
return this.mistake;
}
public String getMessage()
{
return super.getMessage() + getMistake();
}
}
public class Exceptiontest
{
private static int age; public void input() throws Myexception
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
age = Integer.parseInt(stdin.readLine());
if(age<=0&&age>150)
throw new Myexception("系统错误","年龄越界");
} public void d()
{
try
{
input();
}
catch(Myexception e)
{
System.out.println(e.getMessage());
}
          System.out.print("本次登记操作结束");
}
public static void main(String[] args)
{
Exceptiontest mytest = new Exceptiontest();
System.out.print("请输入你的年龄:");
mytest.d();
}
}
此程序的目的是:输入年龄后,如年龄越界则输出错误信息,如在范围之内输出正确信息。在这里我继承了EXCEPTION类,并重写了方法。但是编译时总是出现以下错误,
D:\javatest\Exceptiontest.java:26: unreported exception java.io.IOException; must be caught or declared to be thrown
age = Integer.parseInt(stdin.readLine());
请各位朋友帮我看看是哪里的错误,帮我解决一下问题!!!!!!!!!谢谢!

解决方案 »

  1.   

    stdin.readLine()这句的问题
    public String readLine()
                    throws IOException
    它会抛出IOException,
    你那句public void input() throws Myexception改成public void input() throws Myexception,IOException就好了
      

  2.   

    这个就是你没有catch或者throws掉checked异常造成的。
      

  3.   

    age = Integer.parseInt(stdin.readLine());
    会抛出NumberFormatException
      

  4.   

    readLine会抛出异常,处理一下就可以了