当前位置: 技术问答>java相关
如何应付Java违例不允许我们恢复至违例产生地方?
来源: 互联网 发布时间:2015-04-05
本文导语: //如何应付Java违例不允许我们恢复至违例产生地方? //Inheriting2.java class MyException2 extends Exception{ private int i; public MyException2(){} public MyException2(String msg){ super(msg); } public MyException2(String msg,int x){ super(msg); ...
//如何应付Java违例不允许我们恢复至违例产生地方?
//Inheriting2.java
class MyException2 extends Exception{
private int i;
public MyException2(){}
public MyException2(String msg){
super(msg);
}
public MyException2(String msg,int x){
super(msg);
i = x;
}
}
public class Inheriting2{
public static void f()throws MyException2{
System.out.println("in f()");
throw new MyException2("Originated in f()");
}
public static void g() throws MyException2{
System.out.println("in g()");
throw new MyException2("Originated in g()");
}
public static void h() throws MyException2{
System.out.println("in h()");
throw new MyException2("Originated in h()",47);
}
public static void main(String[] args) throws MyException2{
int i=0;
while(true){
try{
f();
g();
h();
}catch(MyException2 e){
e.printStackTrace();
}finally{
i++;
if(i==3)
break;
}
}
}
}
//其结果是:(我所不希望的)
//in f()
//MyException2: Originated in f()
// at Inheriting2.f(Inheriting2.java:21)
// at Inheriting2.main(Inheriting2.java:35)
//in f()
//MyException2: Originated in f()
// at Inheriting2.f(Inheriting2.java:21)
// at Inheriting2.main(Inheriting2.java:35)
//in f()
//MyException2: Originated in f()
// at Inheriting2.f(Inheriting2.java:21)
// at Inheriting2.main(Inheriting2.java:35)
//----------------------------------------------------
//如果换成这样的话:(太麻烦了,如何改写简单一点的??请高手指点!)
/* 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();
}
}
}
*/
//其结果是:(我想要的情况)
//in f()
//MyException2: Originated in f()
// at I2.f(I2.java:21)
// at I2.main(I2.java:33)
//in g()
//MyException2: Originated in g()
// at I2.g(I2.java:25)
// at I2.main(I2.java:38)
//in h()
//MyException2: Originated in h()
// at I2.h(I2.java:29)
// at I2.main(I2.java:43)
//Inheriting2.java
class MyException2 extends Exception{
private int i;
public MyException2(){}
public MyException2(String msg){
super(msg);
}
public MyException2(String msg,int x){
super(msg);
i = x;
}
}
public class Inheriting2{
public static void f()throws MyException2{
System.out.println("in f()");
throw new MyException2("Originated in f()");
}
public static void g() throws MyException2{
System.out.println("in g()");
throw new MyException2("Originated in g()");
}
public static void h() throws MyException2{
System.out.println("in h()");
throw new MyException2("Originated in h()",47);
}
public static void main(String[] args) throws MyException2{
int i=0;
while(true){
try{
f();
g();
h();
}catch(MyException2 e){
e.printStackTrace();
}finally{
i++;
if(i==3)
break;
}
}
}
}
//其结果是:(我所不希望的)
//in f()
//MyException2: Originated in f()
// at Inheriting2.f(Inheriting2.java:21)
// at Inheriting2.main(Inheriting2.java:35)
//in f()
//MyException2: Originated in f()
// at Inheriting2.f(Inheriting2.java:21)
// at Inheriting2.main(Inheriting2.java:35)
//in f()
//MyException2: Originated in f()
// at Inheriting2.f(Inheriting2.java:21)
// at Inheriting2.main(Inheriting2.java:35)
//----------------------------------------------------
//如果换成这样的话:(太麻烦了,如何改写简单一点的??请高手指点!)
/* 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();
}
}
}
*/
//其结果是:(我想要的情况)
//in f()
//MyException2: Originated in f()
// at I2.f(I2.java:21)
// at I2.main(I2.java:33)
//in g()
//MyException2: Originated in g()
// at I2.g(I2.java:25)
// at I2.main(I2.java:38)
//in h()
//MyException2: Originated in h()
// at I2.h(I2.java:29)
// at I2.main(I2.java:43)
|
do not understand why you want to do this, but try
int i=0;
while(true){
try{
switch (i)
{
case 0:
f();
break;
case 1:
g();
break;
case 2:
h();
break;
default:
break;
}
}catch(MyException2 e){
e.printStackTrace();
}finally{
i++;
if(i==3)
break;
}
}
int i=0;
while(true){
try{
switch (i)
{
case 0:
f();
break;
case 1:
g();
break;
case 2:
h();
break;
default:
break;
}
}catch(MyException2 e){
e.printStackTrace();
}finally{
i++;
if(i==3)
break;
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。