public class ExceptionDemo {
public static void main(String[] args) {
try {
int num=DemoEx.div(4, -2);
System.out.println(num);
} catch (FuShuException e) {
System.out.println(e.toString());
}
}
}
class DemoEx{
public static int div(int x, int y) {
if (y==0) {
throw new ArithmeticException("除数为0啦,哈哈!");
};
if (y<0) {
throw new FuShuException("负数不符合我的要求,自定义抛出错误!");
}
return x/y;
}
}
class FuShuException extends RuntimeException{
/**
*
*/
private static final long serialVersionUID = 1L;
public FuShuException(String message) {
super(message);
}
}
成为异常类的子孙
觉得应该发生的什么的时候,抛出去。
public class SunRuntimeException extends RuntimeException {
}
public class TestSun {
public static void main(String[] args) {
try {
String abc = "在你觉得是这个异常的地方,应用他";
} catch (SunRuntimeException e) {
System.out.println("就是这个了");
}
}
}