日期:2014-05-20  浏览次数:20782 次

这个异常怎么弄啊,求高手。。。
[size=16px][/size]
/*
需求:自定义一个异常,复习并牢牢掌握异常方面的知识。
思路:使用异常,并在代码中体现,异常的继承关系,自定义异常处理代码,运行时异常,try和throws两种处理方式;
步骤:
*/
package exceptiontest;
class AgeException extends Exception
{
private int age;
AgeException(String message,int age)
{
super(message);
this.age=age;
}
public int getAge()
{
return age;
}
}
class NameException extends Exception
{
private String name;
NameException(String message,String name)
{
super(message);
this.name=name;
}
public String getName()
{
return name;
}
}
class Student
{
private String name;
private int age;
public Student(String name,int age) throws AgeException,NameException
{
if(age>25||age<5)
throw new AgeException("年龄不符",age);
  this.age=age;
if(name.length()>5)
throw new NameException("姓名过长",name);
this.name=name;
}
public String getName()
{
try
{
return name;
}
catch(NameException n)
{
System.out.println(n.getMessage());
System.out.println("异常的姓名为:"+n.getName());
name=name.substring(0,5);
System.out.println("自动取其前五个字符");
}
finally 
{
return name;
}
}
public int getAge()
{
try
{
return age;
}
catch(AgeException a )
{
System.out.println(a.getAge());
System.out.println("异常的年龄为:"+a.getAge());
age=20;
System.out.println("年龄已赋值为默认值");
}
finally 
{
return age;
}

}
public void show()
{
System.out.println("姓名:"+this.getName()+" 年龄:"+this.getAge());
}
}
class ExceptionTest
{
public static void main(String []args)
{
Student student=new Student("张三",200);
student.show();
}
}
编译时的错误提示为:

D:\javaprog\731>javac -d . ExceptionTest.java
ExceptionTest.java:52: 错误: 在相应的 try 语句主体中不能抛出异常错误NameException
  catch(NameException n)
  ^
ExceptionTest.java:70: 错误: 在相应的 try 语句主体中不能抛出异常错误AgeException
  catch(AgeException a )
  ^
2 个错误
求大家解释下。。。

------解决方案--------------------
你自己定义的这两个NameException和AgeException使用不当,你try-catch使用的时候,如果这个try里面代码没有任何可能抛出NameException,你在catch捕获NameException是会报编译错误的,给你一个非常简单的自定义异常的实例。

Java code

class AgeException extends Exception
{
    private int age;
    AgeException(String message,int age)
    {
        super(message);
        this.age=age;
    }
    public int getAge()
    {
        return age;
    }
}
class NameException extends Exception
{
    private String name;
    NameException(String message,String name)
    {
        super(message);
        this.name=name;
    }
    public String getName()
    {
        return name;
    }
}
class Student
{
    private String name;
    private int age;
    public Student(String name,int age) throws AgeException,NameException
    {
        if(age>25||age<5)
            throw new AgeException("年龄不符",age);
        this.age=age;
        if(name.length()>5)
            throw new NameException("姓名过长",name);
        this.name=name;
    }
    public String getName()
    {
       
            return name;
       
    }
    public int getAge(){
        
            return age;        

    }
    public void show()    {
        System.out.println("姓名:"+this.getName()+" 年龄:"+this.getAge());
    }
}
public class ExceptionTest{
    public static void main(String []args)
    {
        Student student= null;
        try {
            student = new Student("张三",200);
        } catch (AgeException e) {
            e.printStackTrace();  
        } catch (NameException e) {
            e.printStackTrace();  
        }
        student.show();
    }
}