下面是一个Application程序,其功能是实现简单的自定义异常的功能,当主函数调用f()方法时,引发MyException异常,并输出信息。
 classMyExceptionextendsException
  {
  publicMyException()
  {
  }
  publicMyException(Stringmessage)
  {
  super(message);
  }
  } 
  publicclassExceptionInherit
  {
  publicstaticvoidf()throwsMyException
  {
  System.out.println(″ThrowingMyException″);
  catchnewMyException(″thesecondconstructor!″);
  }
  publicstaticvoidmain(String[]args)
  {
  try
  {
  f();
  }
  catch(Exceptione)
  {
  e.printStackTrace();
  }
  }
  }

解决方案 »

  1.   


    class MyException extends Exception {
    public MyException() {
    } public MyException(String message) {
    super(message);
    }
    }public class ExceptionInherit {

    public static void main(String[] args) {
    try {
    f();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
        public static void  f() throws MyException{
    System.out.println("Throwing MyException");
    throw new MyException("the second constructor!");
        }
    }