自定义异常 重写父类的toString() 不能抛异常 请帮忙,谢谢
package ThrowsExceptionTest;
public class A{
private String name;
public void setName(String name){
this.name = name;
}
public String getName() throws MyException{
if(name==null||name==""){
throw new MyException("名字不能为空");
}
return this.name;
}
public String toString() throws MyException{
String s;
s = this.getName();
return s;
}
void ExceptionTwo(){
throw new NullPointerException("123");
}
public A() {
// TODO Auto-generated constructor stub
}
/**
* @param args
* @throws MyException
*/
public static void main(String[] args) throws MyException {
A a = new A();
a.setName(null);
try{
System.out.println(a.getName());
}catch(NullPointerException e){
throw e;
}finally{
System.out.println("finally@@@");
}
a.ExceptionTwo();
}
}
class MyException extends Exception{
public MyException(String message){
super(message);
}
}