# Exception Handling for Custom Keywords

**URL:** <https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [June 27, 2018, 8:24am UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742 "2018-06-27T08:24:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Praful\_Kumar\_Polimer](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Praful\_Kumar\_Polimer](https://forum.katalon.com/u/Praful_Kumar_Polimer)\
**Post date:** [June 27, 2018, 8:24am UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/1 "2018-06-27T08:24:32Z")

</div>

Hi,

I have created a custom keyword where, if the validation fails, I throw an exception.

I am calling that Custom Keyword in my Test Case that is surrounded in a Try…Catch block.

The Exception thrown in the Custom Keyword is not caught in the Test Case’s catch block.

Could not post the original script but, below is a sample with the same control flow

public class CheckException {  
@Keyword  
def chkEx(){  
boolean check = true  
if (check == false){  
throw new Exception(“Boolean is fasle”)  
}  
}  
}

-------------------------------Test Case-------------------------------

try{

```
   CustomKeywords.'CheckException.chkEx'  

```

} catch (Exception e){  
e.getMessage()  
}

Please let me know how to catch the exception in the main test case?

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [June 27, 2018, 1:03pm UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/2 "2018-06-27T13:03:29Z")

</div>

Perhaps the problem is caused by how Katalon handles the exception stack?

This works fine in my Keyword classes – however, I don’t use @Keyword, instead I create static methods, like this:

```auto
import com.kms.katalon.core.exception.StepErrorException as StepErrorException

public class mytools {

  static void chkBoolean(boolean chk) {
    if(!chk) {
      throw new StepErrorException("Boolean chk is false")
    }
  }

  static void otherMethod() { ... }
}

```

And in my Test Case, I call them like this:

```auto
import static mytools.*

try {
   chkBoolean(whatever)
} catch (Exception e) {
  // do stuff with e
  // maybe throw e again
}

```

In fact, my whole test is contained in a Test class constructor called Test:

```auto
// imports ...

public class Test extends page_from_my_pom {
  Test() {
    navigateTo() // go to page_from_pom

    // more steps here
  }
}

try {
  new Test()
  passThisTest() // static method
} catch (Exception e) {
  failThisTest(e.message) // static method 
  throw e
}

```

It’s a nice enough model giving me pretty good control (before handing control back to Katalon) – I hope it helps you.

---

<div class="post-metadata">

**Author:** ![Praful\_Kumar\_Polimer](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Praful\_Kumar\_Polimer](https://forum.katalon.com/u/Praful_Kumar_Polimer)\
**Post date:** [June 28, 2018, 8:56am UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/3 "2018-06-28T08:56:16Z")

</div>

@Russ Thomas

This works great. Exactly what I was looking for.

Thanks a lot!!!

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [June 28, 2018, 11:38am UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/4 "2018-06-28T11:38:53Z")

</div>

You’re very welcome. And thanks for the BA B)

---

<div class="post-metadata">

**Author:** ![Praful\_Kumar\_Polimer](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Praful\_Kumar\_Polimer](https://forum.katalon.com/u/Praful_Kumar_Polimer)\
**Post date:** [June 29, 2018, 2:23pm UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/5 "2018-06-29T14:23:47Z")

</div>

@Russ Thomas  
Why is it that when I print the exception as it is, it prints the complete stack trace information without any issues.  
But, when I use e.getMessage() or e.Message(), it prints null

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [June 29, 2018, 2:47pm UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/6 "2018-06-29T14:47:20Z")

</div>

I use e.message

---

<div class="post-metadata">

**Author:** ![Praful\_Kumar\_Polimer](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Praful\_Kumar\_Polimer](https://forum.katalon.com/u/Praful_Kumar_Polimer)\
**Post date:** [July 1, 2018, 10:03am UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/7 "2018-07-01T10:03:19Z")

</div>

@Russ Thomas  
e.message doesn’t work as well. It prints null

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [July 1, 2018, 12:43pm UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/8 "2018-07-01T12:43:39Z")

</div>

Show me your try-catch block.

---

<div class="post-metadata">

**Author:** ![Praful\_Kumar\_Polimer](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Praful\_Kumar\_Polimer](https://forum.katalon.com/u/Praful_Kumar_Polimer)\
**Post date:** [July 2, 2018, 6:35am UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/9 "2018-07-02T06:35:11Z")

</div>

It is working now. I had created a User Defined Exception and was trying to call the e.message instead of converting the exception to string.  
Works now with e.toString().

---

<div class="post-metadata">

**Author:** ![Maciej\_Jaros](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/maciej_jaros/32/14067_2.png) [@Maciej\_Jaros](https://forum.katalon.com/u/Maciej_Jaros)\
**Post date:** [October 22, 2018, 4:07pm UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/10 "2018-10-22T16:07:40Z")

</div>

Using `StepErrorException` inside CustomKeyword also works. Throwing any exection works so `throw new Exception(“some message”)` will also work and doesn’t seem to be much of a difference to Katalon.

So this is something I came up with to handle errors in a custom keyword. But Katalon seems to be actually handling failures differently so YMMV…

```auto
@Keyword
String waitUntilVisible(String componentSelector, int timeout = 10, FailureHandling failureHandling = FailureHandling.STOP_ON_FAILURE) {
		KeywordLogger log = new KeywordLogger();
//...
		if (id == null || id.isEmpty()) {
			String message = "Component was not found within ${timeout}s (using query '${componentSelector}')";
			if (failureHandling == FailureHandling.STOP_ON_FAILURE) {
				throw new StepErrorException(message);
			} else if (failureHandling == FailureHandling.CONTINUE_ON_FAILURE) {
				log.logError(message);
			} else {
				log.logWarning(message);
			}
		}
		return id;
}

```

---

<div class="post-metadata">

**Author:** ![jmanzanares](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jmanzanares](https://forum.katalon.com/u/jmanzanares)\
**Post date:** [February 17, 2020, 9:25am UTC](https://forum.katalon.com/t/exception-handling-for-custom-keywords/11742/11 "2020-02-17T09:25:57Z")

</div>

Hi  
I’m encountering StepErrorException even I already marked it as passed.  
Here’s my code.

public def Boolean isElementPresent(String xpath) {  
try {  
this.wait = new WebDriverWait(this.driver, 10);  
WebElement element = this.driver.findElement(By.xpath(xpath));  
return true;  
}  
catch(org.openqa.selenium.NoSuchElementException ex) {  
return false  
}  
catch(Exception ex) {  
return false;  
}  
}

It already passed when I marked it as Passed but it returns an error  
2020-02-17 17:17:40.735 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - ❌ com.kms.katalon.core.exception.StepErrorException: Expected condition failed: waiting for presence of element located by: By.xpath: //app-iic-reviews//div[contains(@class,‘card border rounded’)] (tried for 10 second(s) with 500 milliseconds interval)
