Java 实例 - 自定义异常

Java 教程 · 2019-02-11 11:29:21

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

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 教程 文章: https://www.codercto.com/courses/l/12.html

查看所有标签

Scalability Rules

Scalability Rules

Martin L. Abbott、Michael T. Fisher / Addison-Wesley Professional / 2011-5-15 / USD 29.99

"Once again, Abbott and Fisher provide a book that I'll be giving to our engineers. It's an essential read for anyone dealing with scaling an online business." --Chris Lalonde, VP, Technical Operatio......一起来看看 《Scalability Rules》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具