/**
* <p>title: 捕获异常和实现自己的异常类</p>
* <p>description: 通过继承exception类来实现自己的异常类。并使用try-catch来捕获这个异常。</p>
* <p>copyright: copyright (c) 2003</p>
* <p>filename: </p>
* @version 1.0
*/
class myexception extends exception {
public myexception() {}
public myexception(string msg) {
super(msg);
}
public myexception(string msg, int x) {
super(msg);
i = x;
}
public int val() { return i; }
private int i;
}
public class demoexception {
/**
*<br>方法说明:使用myexception类中默认的构造器
*<br>输入参数:
*<br>返回类型:
*/
public static void a() throws myexception {
system.out.println(
"throwing myexception from a()");
throw new myexception();
}
/**
*<br>方法说明:使用myexception类中带信息的构造器
*<br>输入参数:
*<br>返回类型:
*/
public static void b() throws myexception {
system.out.println(
"throwing myexception from b()");
throw new myexception("originated in b()");
}
/**
*<br>方法说明:使用了myexception中有编码的构造器
*<br>输入参数:
*<br>返回类型:
*/
public static void c() throws myexception {
system.out.println(
"throwing myexception from c()");
throw new myexception(
"originated in c()", 47);
}
public static void main(string[] args) {
try {
a();
} catch(myexception e) {
e.getmessage();
}
try {
b();
} catch(myexception e) {
e.tostring();
}
try {
c();
} catch(myexception e) {
e.printstacktrace();
system.out.println("error code: " + e.val());
}
}
} //end :)
闽公网安备 35060202000074号