本帖最后由 杨希 于 2014-4-4 11:23 编辑
代码如下
- class FuShuException extends Exception
- {}
- class Demo
- {
- int div(int a,int b)throws FuShuException
- {
- if (b<0)
- throw new FuShuException();
-
- return a/b;
- }
- }
- class ExceptionDemo
- {
- public static void main(String args[])
- {
- Demo d = new Demo();
- try
- {
- int x = d.div(4,-1);
- System.out.println("x="+x);
- }
- catch(FuShuException e)
- {
- System.out.println(e.toString());
- System.out.println(e.getMessage());
- }
-
- System.out.println("over");
- }
- }
复制代码
1,当b=0时,抛出异常e.toString()和e.getMessage(),得到的结果一样,getMessage()不是返回的是throwable的详细消息字符串,toString返回的是throwable的简短描述吗?两个结果都是这个
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Demo.div(ExceptionDemo.java:10)
at ExceptionDemo.main(ExceptionDemo.java:20)
2,当b<0时,
e.toString()的结果是 :
FuShuException over
e.getMessage()的结果是 :
null
over
能帮我具体解释一下吗?谢谢了!
|