Trying to catch the WebElementNotFoundException exception unsuccessfully

I’ve been trying to further improve the custom logging I use in my testcases. What I would like to be able to catch each exception and log it appropriately somewhere, so I can later look back at what went wrong in a few simple sentences.

try {
    TestCase.runTest()
} catch(WebElementNotFoundException e) {
    println(e.message)
} catch(StepFailedException e) {
    println(e.message)
} catch(Exception e) {
    println(e.message)
}

The TestCase.runTest() is just a collection of WebUI.callTestCase methods.

In the logs I can see there are multiple kind of exception, one of which is the WebElementNotFoundException. However the try/catch block above only catches the StepFailedException or the general Exception.

That error message about the specific selector being wrong is the one that I want to use in my custom logging.

How would I go about to achieve this result?

The reason your try/catch block is reporting back the StepFailedException is that it is the top-most exception. It’s then printing down the stack to the root cause, which is your WebElementNotFoundException.

If the top-level exception you got was a WebElementNotFoundException, that’s what would get caught instead of the StepFailedException.

I’m thinking the way to handle this would be something like:

1.) Catch ANY exception.
2.) Find the root exception and handle it (report it or whatever) if it matches one of the types you care about.

In Java:

try {
    // something
} catch (Exception e) {
    while(e.getCause() != null) {
        e = e.getCause();
    }
    String exceptionName = e.getClass().getSimpleName();
    if (exceptionName.equals("StepFailedException")) {
        // do something
    } else if (exceptionName.equals("WebElementNotFoundException")) {
        // do something else
    }
    // add more else ifs for exceptions you care about...
}

You could also do this iteratively down the stack by putting everything into the while loop if you wanted to handle multiple/all exceptions in the stack:

try {
    // something
} catch (Exception e) {
    while(e.getCause() != null) {
        e = e.getCause();
        String exceptionName = e.getClass().getSimpleName();
        if (exceptionName.equals("StepFailedException")) {
            // do something
        } else if (exceptionName.equals("WebElementNotFoundException")) {
            // do something else
        }
        // add more else ifs for exceptions you care about...
    }
}
1 Like

This is exactly what I needed! Thank you very much. I am not experienced enough with Java, so I am missing some crucial know-hows.

I used the iterative version and it worked perfectly.

The only addition I would like to make is that when you’re conditioning the exception names it expects the full class name like so: com.kms.katalon.core.webui.exception.WebElementNotFoundException

Thanks again, especially for the fast response!

Oh right, that will give you the package as well. You could use getSimpleName() instead, that will just give the class name.

Glad it worked for you! I will mark my answer as the solution.