package 异常和断言;标签:java,String,自定义,super,getMessage,msg,异常 From: https://blog.51cto.com/u_9368800/5868089
public class 自定义异常 {
public static void main(String[] args) {
try{
//抛出自定义异常,fd为异常信息
throw new d("fd");
}//捕捉自定义异常
catch(d e){
//调用getMessage方法
System.out.println(e.getMessage());
}//用finally输出最后一句话
finally{
System.out.println("This is my first Exception!Am I clever?Please pass \"like\"!");
}
}
}
//自定义异常,继承Throwable
class d extends Throwable{
//用msg接收异常信息
String msg;
//继承Throwable的几个构造方法
d(){
super();
}
d(String msg){
super(msg);
//接收异常信息
this.msg=msg;
}
d(String msg,Throwable s){
super(msg,s);
}
d(Throwable s){
super(s);
}
//覆盖getMessage方法
public String getMessage(){
//返回msg,异常信息
return msg;
}
}