throw new FuShuException("出现了除数为负数");
上面一句的最后的分号为中文的分号,改成;
另外当b=0时,return a/b;会报除零异常,应该捕获- class FuShuException extends RuntimeException
- {
- FuShuException(String msg)
- {
- super(msg);
- }
- }
- class Demo
- {
- int div(int a,int b)
- {
- if(b<0)
- throw new FuShuException("出现了除数为负数");
- if(b==0)
- throw new ArithmeticException("被零除了");
- return a/b;
- }
- }
- class LinLeiException
- {
- public static void main(String[] args)
- {
- Demo d=new Demo();
- int x=0;
- try{x=d.div(4,0);}catch(ArithmeticException e){System.out.println(e.getMessage());}
- System.out.println("x="+x);
- System.out.println("over");
- }
- }
复制代码 |