java自定义异常

时间:2022-09-30 00:42:17
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\"!");}
}
}//自定义异常,继承Throwableclass 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;}}