import java.io.*;
class B extends Exception{
public B(String name){
super(name);
}
}
class divide {
public double f(double a,double b) throws B//这个表示这个方法对异常不进行处理,传给上一级进行处理
//处理的方式是try{}catch{}来进行,line27~line31
{
double m = 0;
if (b == 0)
throw new B("除数为0,gg了");
else
m = a/b;
return m;
}
}
public class define_error {
public static void main(String[] args){
divide bb = new divide();
try {
bb.f(6, 0);
}
catch(Exception e){
e.printStackTrace();
}
}
}
自定义异常的例子
最新推荐文章于 2022-01-07 22:03:31 发布