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://codercto.com/courses/l/12.html

查看所有标签

Mathematica Cookbook

Mathematica Cookbook

Sal Mangano / O'Reilly Media / 2009 / GBP 51.99

As the leading software application for symbolic mathematics, Mathematica is standard in many environments that rely on math, such as science, engineering, financial analysis, software development, an......一起来看看 《Mathematica Cookbook》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码