Try & Catch doesnt catch StepFailedException

Before hitting that Ask the Community button, make sure to:

Operating System

Windows 10

Katalon Studio version

e.g. version 8.6.5

Code:

try {
	
	boolean check = WebUI.verifyElementNotPresent(findTestObject('Ophthapraxis/OP-Planung/Behandlung erstellen/FehlermeldungAbstand', [('formattedDate') : formattedDate]),
		2)
	 if(check) {
	WebUI.closeBrowser()
}

else {

//something
}
}
catch (StepFailedException e)
{

}

Log :

=============== ROOT CAUSE =====================

For trouble shooting, please visit: https://docs.katalon.com/katalon-studio/docs/troubleshooting.html

08-10-2023 01:54:38 PM check = verifyElementNotPresent(findTestObject(“Ophthapraxis/OP-Planung/Behandlung erstellen/FehlermeldungAbstand”, [“formattedDate”:formattedDate]), 2)

Elapsed time: 2,749s

Web element with id: ‘Object Repository/Ophthapraxis/OP-Planung/Behandlung erstellen/FehlermeldungAbstand’ located by ‘By.xpath: //p[text()=‘Für den 02.01.2023 kann keine IVOM-Behandlung geplant werden, da der vertraglich erforderliche Mindestabstand von 26 Tagen zu einer anderen IVOM-Behandlung nicht eingehalten werden würde.’]’ is present after ‘2’ second(s) (Root cause: com.kms.katalon.core.exception.StepFailedException: Web element with id: ‘Object Repository/Ophthapraxis/OP-Planung/Behandlung erstellen/FehlermeldungAbstand’ located by ‘By.xpath: //p[text()=‘Für den 02.01.2023 kann keine IVOM-Behandlung geplant werden, da der vertraglich erforderliche Mindestabstand von 26 Tagen zu einer anderen IVOM-Behandlung nicht eingehalten werden würde.’]’ is present after ‘2’ second(s)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain$stepFailed$0.call(Unknown Source)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementNotPresentKeyword$_verifyElementNotPresent_closure1.doCall(VerifyElementNotPresentKeyword.groovy:113)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementNotPresentKeyword$_verifyElementNotPresent_closure1.doCall(VerifyElementNotPresentKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain$runKeyword.call(Unknown Source)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementNotPresentKeyword.verifyElementNotPresent(VerifyElementNotPresentKeyword.groovy:122)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementNotPresentKeyword.execute(VerifyElementNotPresentKeyword.groovy:68)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementNotPresent(WebUiBuiltInKeywords.groovy:1497)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementNotPresent$3.call(Unknown Source)
at VorgangErstellen.run(VorgangErstellen:127)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
at com.kms.katalon.core.common.CommonExecutor.accessTestCaseMainPhase(CommonExecutor.java:65)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:148)
at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:106)
at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:187)
at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
at TempTestSuite1691668444014.run(TempTestSuite1691668444014.groovy:36)
)

Environment (for Web Testing)

Firefox 115.1.0esr (64-Bit)

Steps to reproduce

Build a try & catch block with a StepFailedException inside the try and write your catch like:
catch (StepFailedException e) {
}

(I have tried catch with different Exception Conditions like here Exception Handling Statements | Katalon Docs)

Expected Behavior
It will catch the Exception and will go on.

Actual Behavior

It does throw a Exception and stops.

StepFailedException and StepErrorException — these are 2 distict Exception classes. You need to be careful which class you expect.

See
https://api-docs.katalon.com/com/kms/katalon/core/exception/package-summary.html

I use the StepFailedException as I stated because I want to use the verifyElementNotPresent Action and if it catches something, it should just go on.

This explanation is ambiguous to others, though it may be clear enough for you.
The code matters.
Please show a runnable TestCase code that you see problematic.

I updated the post, thank you for the tip.

You should make use of the 3rd argument “flowControl” to
Mobile.verifyElementPresent(TestObject, timeout, flowControl)

See

The default of the flowControl is set “FailureHandling.STOP_ON_FAILURE”. So the keyword will throw Exception when “the element is still present after 2 seconds”.

If you explicity specify “FailureHandling.OPTIONAL” , the keyword will never throw Exception even if the condition was found true after the timeout. That is what you want to implement.

With failureHandling = OPTIONAL, You do not need to write try (xxx) {} catch {} block; as no Exception will be thrown.

Thanks for the tip. The Problem is I want to use the verifyElementNotPresent Action for a boolean condition. If the action throws the exception it will mean, that it turned out true and with the true I want to activate a if condition like I stated in my code. I want to close the browser then for example. But it doesnt identify the action as true or false if I use the FailureHandling.OPTIONAL.

No, you are wrong. As described in the doc, the keyword will return a boolean value even when you specify FailureHandling.OPTIONAL.

Please give it a chance.