本帖最后由 Up↑Lee↗ 于 2014-3-24 11:31 编辑
- class FuShuException extends Throwable //Exception // Exception的父类是Throwable,为什么FuShuException直接继承Exception就可以呢??
- { //为什么总觉得应该继承Throwable,又为什么继承两者的结果一样呢?
- private String msg; //下方注释程序不正说明如此么?
- FuShuException(String msg)
- {
- super(msg);
-
- }
- }
- class Demo
- {
- int div(int a,int b)throws FuShuException
- {
- if(b<0)
- throw new FuShuException("出现了除数是负数的情况------ / by fushu");//手动通过throw关键字抛出一个自定义异常对象。
- return a/b;
- }
- }
- class ExceptionDemo3
- {
- public static void main(String[] args)
- {
- Demo d = new Demo();
- try
- {
- int x = d.div(4,-9);
- System.out.println("x="+x);
- }
- catch (FuShuException e)
- {
- System.out.println(e.toString());
- }
-
- System.out.println("over");
- }
- }
- /*
- class Throwable
- {
- private String message;
- Throwable(String message)
- {
- this.message = message;
- }
- public String getMessage()
- {
- return message;
- }
- }
- class Exception extends Throwable
- {
- Exception(String message)
- {
- super(message);
- }
- }
- */
复制代码 问题在注释中
|