自定义异常

来源:互联网 发布:深入理解java构造器 编辑:程序博客网 时间:2024/06/10 16:46

通过继承Exception写自己定义的异常

public class MyException extends Exception{
//自己定义的异常类,可以输出异常发生的原因提示 public MyException(String name) { super(name); }  public static void main(String args[]) throws MyException{ Integer i = new Integer(-1);  if(i < 0) { throw new MyException("my exception:Integer < 0"); } }}

0 0