【问题标题】:how to avoid Infinite Recursion in a non-return method with Try catch如何使用 Try catch 在非返回方法中避免无限递归
【发布时间】:2020-09-09 18:20:22
【问题描述】:

公共类示例{

public static void main(String[] args) {

    method();

}

public static void method()
{
    try {
        System.out.println("function");
        throw new StaleElementReferenceException("thih sexception occured");
    }
    catch (StaleElementReferenceException e) {
        method();
    }
    catch (Exception e) {
        System.out.println("AssertFail");
    }
}

}

如何使用 Try catch 在非返回方法中避免无限递归...例如下面的代码...当 StaleElementException 仅发生一次时,我想执行“异常之后的函数,如果 Stale Element 第二次发生我希望它去异常捕获并打印断言失败..如何?

【问题讨论】:

    标签: java selenium staleelementreferenceexception


    【解决方案1】:
    public class Sample {
    
    public static void main(String[] args) {
    
        method(false);
    
    }
    
    public static void method(boolean calledFromCatchBlock)
    {
        try {
            System.out.println("function");
            if(!calledFromCatchBlock) {
                throw new StaleElementReferenceException("thih sexception occured");
            } else {
                throw new Exception();
            }
        } catch (StaleElementReferenceException e) {
            method(true);
        } catch (Exception e) {
            System.out.println("AssertFail");
        }
    }
    }
    

    【讨论】:

    • thnx,有想过这个,但不想每次调用方法时都传递 false ......这是不添加/修改方法参数列表的规则......跨度>
    • 然后维护一个静态变量作为标志。
    【解决方案2】:

    当您在method() 之外抛出异常(例如boolean 标志)时,您应该以某种方式存储状态,检查此状态并在下次抛出修改后的异常:

    private static boolean alreadyThrown = false;
    
    public static void method()
    {
        try {
            System.out.println("function");
            if (alreadyThrown) {
                throw new RuntimeException("another exception occured");
            } else {
                alreadyThrown = true;
                throw new StaleElementReferenceException("this exception occured");
            }
        }
        catch (StaleElementReferenceException e) {
            method();
        }
        catch (Exception e) {
            System.out.println("AssertFail");
        }
    }
    

    或者您可以为method(int arg) 提供一些参数并以类似的方式检查其值:

    public static void main(String[] args) {
        method(1);
    }
    
    public static void method(int arg)
    {
        try {
            System.out.println("function");
            if (arg > 1) {
                throw new RuntimeException("another exception occured");
            } else {
                throw new StaleElementReferenceException("this exception occured");
            }
        }
        catch (StaleElementReferenceException e) {
            method(arg + 1);
        }
        catch (Exception e) {
            System.out.println("AssertFail");
        }
    }
    

    【讨论】:

    • Thnx,第一个带有标志的解决方案很有帮助
    • 实际上 syso("function") 行会抛出 stale element 异常......你可以忽略我的代码中 staleElementReference 行的抛出............sry。 ..didnt relize...
    • 而且这个函数很长,在try里面写一次,在else里面写一次......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2012-02-02
    • 2020-11-10
    • 2013-06-05
    相关资源
    最近更新 更多