Java 实例 - 自定义异常

黄舟
黄舟 原创
2017-02-04 10:22:33 1130浏览

以下实例演示了通过继承 Exception 来实现自定义异常:

/*
 author by w3cschool.cc
 TestInput.java
 */

class WrongInputException extends Exception {
   WrongInputException(String s) {
      super(s);
   }
}
class Input {
   void method() throws WrongInputException {
      throw new WrongInputException("Wrong input");
   }
}
class TestInput {
   public static void main(String[] args){
      try {
         new Input().method();
      }
     catch(WrongInputException wie) {
         System.out.println(wie.getMessage());
      }
   } 
}

以上代码运行输出结果为:

Wrong input

以上就是Java 实例 - 自定义异常的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。