java自定义异常

来源:互联网 发布:如何恢复sd卡数据 编辑:程序博客网 时间:2024/06/16 09:27
package 异常和断言;

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 Iclever?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;
}
}