Java 异常后继续执行

Java 异常后继续执行,java,exception,loops,Java,Exception,Loops,我有两门课: public class TryException { int a=0; TryException(int c) { a = c; } public boolean operation() //just example { if(a!=10) { System.out.println(a); return true; }else

我有两门课:

public class TryException {
    int a=0;

    TryException(int c) {
        a = c;
    }

    public boolean operation() //just example
    {
        if(a!=10)
        {
            System.out.println(a);
            return true;
        }else{
            throw new RuntimeException("display something");
        }

    }

}
主要问题是:

public class Test {
    static public void main(String args[])
    {
        int val =20;
        TryException ex = new TryException(val);

        try{
            while(ex.operation()){
                ex.a = --val;
            }

        }catch(RuntimeException e)
        {
            System.out.println("try exception");
        }
    }
}
当我运行这个程序时,当它检测到异常时,执行就会停止。如何在异常后继续执行相同的

public class Test {
    static public void main(String args[])
    {
        int val =20;
        TryException ex = new TryException(val);

        boolean status = true;
        while(status){
            try{
                 status = ex.operation();
            } catch(RuntimeException e) {
                status = true; //Or whatever...
            }
            ex.a = --val;
        }
    }
}
这可能会有帮助

public class Test {
    static public void main(String args[])
    {
        int val =20;
        TryException ex = new TryException(val);

        boolean status = true;
        while(status){
            try{
                 status = ex.operation();
            } catch(RuntimeException e) {
                status = true; //Or whatever...
            }
            ex.a = --val;
        }
    }
}

将试扣移动到环内

 boolean run = true;
 while(run){
    ex.a = --val;
    try{
       run = ex.operation();
    }catch(RuntimeException e){
        System.out.println("try exception");
    }    
 }

您需要决定何时将
run
设置为
false

在循环中移动try catch

 boolean run = true;
 while(run){
    ex.a = --val;
    try{
       run = ex.operation();
    }catch(RuntimeException e){
        System.out.println("try exception");
    }    
 }

您需要决定何时将
run
设置为
false

将您的try/catch移动到while内。为什么将try/catch移动到while内?为什么将try/catch移动到while内?您知道,您的示例中有无限循环吗?将while(true)更改为while(status)对不起,我错了。我忘了改了。你知道吗,你的例子中有无限循环?将while(true)更改为while(status)对不起,我错了。我忘了换过了。