这个异常怎么弄啊,求高手。。。
[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(); }}
[解决办法]
以下是我将LZ代码修改过后的,运行时没问题:
- Java code
/*姓名异常*/public class NameException extends Exception{ private static final long serialVersionUID = 1L; public NameException(String message){ super(message); } public static long getSerialVersionUID() { return serialVersionUID; }}/*年龄异常*/public class AgeException extends Exception{ private static final long serialVersionUID = 1L; public AgeException(String message){ super(message); } public static long getSerialVersionUID() { return serialVersionUID; } }/*Person*/public class Person { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person(String name,int age) throws NameException, AgeException{ if(name.length()>5){ throw new NameException("name.length过长"); }else{ this.name = name; } if(age>50||age<15){ throw new AgeException("age不符合标准"); }else{ this.age = age; } }}/*测试类*/public class ExceptionTest { public static void main(String[] args) throws NameException, AgeException { Person per = new Person("asdasd",20); }}
[解决办法]
这个需要程序员catch异常之后 自己控制打印输出,age name参数都已经传到自定义异常的构造方法当中
输出出来就行了
- Java code
package csdn;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) { System.out.println(e.getMessage()+",异常年龄是"+e.getAge());// e.printStackTrace(); } catch (NameException e) { System.out.println(e.getMessage()+",异常姓名是"+e.getName());// e.printStackTrace(); } student.show(); }} 