日期:2014-05-20 浏览次数:20979 次
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();
}
}