以下实例演示了通过继承 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 实例 - 自定义异常的内容。