62,615
社区成员
发帖
与我相关
我的任务
分享
class MyException2 extends Exception //建立自定义异常
{
private int x;
public MyException2(){} //构造方法
public MyException2(String msg){super(msg);}
public MyException2(String msg,int x)
{
super(msg);
this.x=x;
}
public int val(){return x;}
public String getMessage()
{
return "Detail Message: "+x+" "+super.getMessage();
}
}
public class ExtraFeatures
{
public static void f() throws MyException2
{
System.out.println("Throwing MyException2 from f()");
throw new MyException2();
}
public static void g() throws MyException2
{
System.out.println("Throwing MyException2 from g()");
throw new MyException2("Originated in g()");
}
public static void h() throws MyException2
{
System.out.println("Throwing MyException2 from h()");
throw new MyException2("Originated in h()",47);
}
public static void main(String[] args)
{
try
{
f();
}catch(MyException2 e){
e.printStackTrace();
}
try
{
g();
}catch(MyException2 e){
e.printStackTrace();
}
try
{
h();
}catch(MyException2 e){
e.printStackTrace();
System.out.println("e.val()="+e.val());
}
}
}