Java 实例 – 自定义异常

Java 实例 – 自定义异常

Java 实例

以下实例演示了通过继承 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 实例

版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《Java 实例 – 自定义异常》
文章链接:https://zhuji.vsping.com/294862.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。