就像除数不能为0 一样,我们自己定义的除数不能为负数为什么不能也自动抛出?除数为0的arithmeticexception为什么会自动抛出?
- class ExceptionTest
- {
-
- public static void main(String[] args)
- {
- Demo d = new Demo();
- int x = d.div(5,-2);
- System.out.println(x);
- }
- }
- class FushuException extends RuntimeException
- {
- FushuException(String message)
- {
- super(message);
- }
- }
- class Demo
- {
- int div(int a,int b)
- {
- if(b<0)
- throw new FushuException("除数不能为负数");
- return a/b;
- }
- }
复制代码
|