Home > Blockchain >  Throw new Exception and have a block where one catches any exception
Throw new Exception and have a block where one catches any exception

Time:02-06

private WebElement findElementByXpath(WebDriver driver, String xpath) throws WebElementNotFoundException, HopelessAccountException {

    WebElement element = null;

    try {
        element = new WebDriverWait(driver, Duration.ofSeconds(dirationInSeconds))
                .until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
    } catch (TimeoutException timeoutException) {
        loggingService.timeMark("findElementByXpath", "TimeoutException");
        throw new WebElementNotFoundException();
    } catch (UnhandledAlertException alertException) {
        loggingService.timeMark("findElementByXpath", "alertException");

        final String LIMITS_EXHAUSTED_MESSAGE = "Not enough limits!";
        String message = alertException.getMessage();

        if (message.contains(LIMITS_EXHAUSTED_MESSAGE)){
            throw new HopelessAccountException(); // Attention.
        }

    } catch (Exception e) {
        // Mustn't be here.
        loggingService.timeMark("findElementByXpath", e.getMessage());
        driver.quit();
        System.out.println("QUIT!");
        System.exit(0);
    }


    loggingService.timeMark("findElementByXpath", "end. Xpath: "   xpath);

    return element;
}

Please, have a look at the line that I commented as "Attention".

I have caught the exception where there is not enough limits any more. And I throw the exception that the account is hopeless.

But it is immediately caught by just after the next few lines. Namely where I commented "Mustn't be here".

I would like to preserve this catching any exception. At least for debugging purpose.

Could you help me understand whether I can both throw HopelessAccountException and preserve the "catch Exception" block?

CodePudding user response:

You can always modify your Exception block to rethrow e if it is an instance of HopelessAccountException:

} catch (Exception e) {

    if (e instanceof HopelessAccountException) throw e;  // preserves original stack trace

    // Mustn't be here.
    loggingService.timeMark("findElementByXpath", e.getMessage());
    driver.quit();
    System.out.println("QUIT!");
    System.exit(0);
}

However as @fishinear indicates, in your posted code the Exception block would not be reached as a result of the throw of throw new HopelessAccountException() - if your actual code looked more like:

    try {
        try {
            System.out.println("In A()");
            // do something to cause an exception E3 (e.g. UnhandledAlertException)
            throw new E3();
        } catch (E3 e3) {  // UnhandledAlertException
            System.out.println("In E3 catch");
            throw new E1(); // HopelessAccountException
        }
    } catch (Exception e) {
        System.out.println("In Exception catch");
        if (e instanceof E1) throw e; // rethrow HopelessAccountException
        System.out.println("e: " e);
    }

Then the test-and-rethrow is possible.

Then when you rip out your debugging "try block" your code would behave the same (for the HopelessAcountException).

CodePudding user response:

in your code that calls findElementByXpath(…) you could catch the broad Exception type there. This means in your findElementByXpath(…) method you could just handle the known exceptions and anything else could be captured in calling code

  •  Tags:  
  • java
  • Related