Java 实例 - 自定义异常
以下实例演示了通过继承 Exception 来实现自定义异常:
TestInput.java 文件
class WrongInputException extends Exception {
WrongInputException(String s) {
super(s);
}
}
class Input {
void method() throws WrongInputException {
throw new WrongInputException("Wrong input");
}
}
class Main {
public static void main(String[] args) {
try {
new Input().method();
} catch(WrongInputException wie) {
System.out.println(wie.getMessage());
}
}
}
WrongInputException(String s) {
super(s);
}
}
class Input {
void method() throws WrongInputException {
throw new WrongInputException("Wrong input");
}
}
class Main {
public static void main(String[] args) {
try {
new Input().method();
} catch(WrongInputException wie) {
System.out.println(wie.getMessage());
}
}
}
以上代码运行输出结果为:
Wrong input
尊贵的董事大人
英文标题不为空时 视为本栏投稿
需要关键字 描述 英文标题