# Error Handling using If else

**URL:** <https://forum.katalon.com/t/error-handling-using-if-else/66563>\
**Category:** Miscellaneous\
**Created:** [July 4, 2022, 12:03pm UTC](https://forum.katalon.com/t/error-handling-using-if-else/66563 "2022-07-04T12:03:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShivaRaju](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/shivaraju/32/16104_2.png) [@ShivaRaju](https://forum.katalon.com/u/ShivaRaju)\
**Post date:** [July 4, 2022, 12:03pm UTC](https://forum.katalon.com/t/error-handling-using-if-else/66563/1 "2022-07-04T12:03:38Z")

</div>

Test case should pass even the condition is in the if statement is true and test cases should fail if the condition is in the else statement is true, but in both the cases test cases are going green

if(Deletedmsg.contains(“is deleted successfully”)) {  
System.out.println(“DataSet is deleted and testcase is sucessful”+Deletedmsg);   
}

```
else {
	FailureHandling.STOP_ON_FAILURE
	System.out.println("DataSet is not deleted and testcase is fail"+Deletedmsg);
}
WebUI.click(findTestObject('Object Repository/Historical/deleted_Sucessful_popup_Ok'))

```

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [July 4, 2022, 4:12pm UTC](https://forum.katalon.com/t/error-handling-using-if-else/66563/2 "2022-07-04T16:12:05Z")

</div>

> [@ShivaRaju](#):
>
> `FailureHandling.STOP_ON_FAILURE`

The above phrase will **not** cause your test case to fail if you have it by itself. The STOP\_ON\_FAILURE is just a **defined reference** within the “FailureHandling” routine. By itself, the STOP\_ON\_FAILURE does not do anything. As an example, try this:

```auto
import com.kms.katalon.core.model.FailureHandling as FailureHandling

println("The value is " + FailureHandling.STOP_ON_FAILURE)

```

In order for the defined reference to be effective, it has to be within a **parameter list** of a Katalon Studio statement. When the phrase is within a parameter list, then the “FailureHandling” method is called. It’s within the “FailureHandling” method that the appropriate action is done. So, if you want your test case to fail, then perhaps you can try:

```auto
else {
    println("DataSet is not deleted and testcase is fail ${Deletedmsg}");
	WebUI.verifyTextPresent("Martin is King!", false, FailureHandling.STOP_ON_FAILURE)
}
WebUI.click(findTestObject('Object Repository/Historical/deleted_Sucessful_popup_Ok'))

```

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 4, 2022, 11:33pm UTC](https://forum.katalon.com/t/error-handling-using-if-else/66563/3 "2022-07-04T23:33:17Z")

</div>

KeywordUtil would be useful for you.

[https://api-docs.katalon.com/com/kms/katalon/core/util/KeywordUtil.html](https://api-docs.katalon.com/com/kms/katalon/core/util/KeywordUtil.html)

Try copy&paste the following and run it.

```auto
import com.kms.katalon.core.util.KeywordUtil

// String Deletedmsg = '''is deleted succesfully'''
String Deletedmsg = '''is inserted accidentaly'''

if (Deletedmsg.contains("is deleted successfully")) {
    System.out.println("DataSet is deleted and testcase is sucessful: " + Deletedmsg);
}
else { // See https://api-docs.katalon.com/com/kms/katalon/core/util/KeywordUtil.html
    String msg = "DataSet is not deleted and testcase failed: " + Deletedmsg
    KeywordUtil.markFailedAndStop(msg)
    //KeywordUtil.markFailed(msg)
    //KeywordUtil.markWarning(msg)
    //KeywordUtil.logInfo(msg)
}
System.out.println("FIN")

```

Please try other methods of KeywordUtil and see how they work.

---

<div class="post-metadata">

**Author:** ![ShivaRaju](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/shivaraju/32/16104_2.png) [@ShivaRaju](https://forum.katalon.com/u/ShivaRaju)\
**Post date:** [July 5, 2022, 9:07am UTC](https://forum.katalon.com/t/error-handling-using-if-else/66563/4 "2022-07-05T09:07:33Z")

</div>

Thank you for your support

---

<div class="post-metadata">

**Author:** ![vu.tran](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/vu.tran/32/20584_2.png) [@vu.tran](https://forum.katalon.com/u/vu.tran)\
**Post date:** [July 5, 2022, 9:56am UTC](https://forum.katalon.com/t/error-handling-using-if-else/66563/5 "2022-07-05T09:56:29Z")

</div>

Thank you for your great support @kazurayam and @grylion54. If this work for you, please mark one of the comment as a possible solution for your problem @ShivaRaju. Thank you all for all the contributions to Katalon Community.
