How can I capture exception through execution

Hi
I’m facing a problem while I got an exception. I’m working on a desktop Winforms Apps and when any exception occur I need to “Exit” out from the application. But I need to login after that if I was unable to exit out then it will through another exception “Multipe Instances Forbidden” - then I need to click “OK” then “Exit” button.

image

I write the code using if statement but the code is only working when " “Multipe Instances Forbidden” is existig in current page.
please see the code.

WebElement okbtn = Windows.getDriver().findElementByName(“OK”)
if (okbtn.isDisplayed() && okbtn.isEnabled()){
Windows.delay(1)
Windows.getDriver().findElementByName(“OK”).click()
Windows.delay(2)
Windows.switchToWindowTitle(‘Colt Smart Client’)
Robot robot = new Robot()
Thread.sleep(400)
robot.keyPress(KeyEvent.VK_ALT)
Thread.sleep(300)
robot.keyPress(KeyEvent.VK_F4)
Thread.sleep(300)
robot.keyRelease(KeyEvent.VK_F4)
Thread.sleep(300)
robot.keyRelease(KeyEvent.VK_ALT)
Thread.sleep(300)
Thread.sleep(10000)
Windows.startApplication(“D:\Program Files (x86)\\Colt Smart Client.exe”)
Windows.delay(5)
Windows.switchToWindowTitle(‘Login Page’)
}else{
Windows.startApplication(“D:\Program Files (x86)\\Colt Smart Client.exe”)
Windows.delay(5)
Windows.switchToWindowTitle(‘Login Page’)
}
Please give a solution.

Hi @wfskatalon01,

You could always use try { … } catch { … } to capture exceptions during the execution. Besides, Katalon Studio also provides its own way to handle exceptions by passing flowControl parameter to any keyword (FailureHandling).
So, if I understand right, your script will look like this:

WebElement okbtn = null
try {
    okbtn = Windows.getDriver().findElementByName('OK')
} catch (exception) {
    // There is no element with the name 'OK'
}

if (okbtn != null && okbtn.isDisplayed() && okbtn.isEnabled()) {
    Windows.delay(1)
    Windows.getDriver().findElementByName('OK').click()
    Windows.delay(2)
    Windows.switchToWindowTitle('Colt Smart Client')
    Robot robot = new Robot()
    Thread.sleep(400)
    robot.keyPress(KeyEvent.VK_ALT)
    Thread.sleep(300)
    robot.keyPress(KeyEvent.VK_F4)
    Thread.sleep(300)
    robot.keyRelease(KeyEvent.VK_F4)
    Thread.sleep(300)
    robot.keyRelease(KeyEvent.VK_ALT)
    Thread.sleep(300)
    Thread.sleep(10000)
    Windows.startApplication('D: \Program Files(x86)\\ Colt Smart Client.exe')
    Windows.delay(5)
    Windows.switchToWindowTitle('Login Page')
} else {
    Windows.startApplication('D: \Program Files(x86)\\ Colt Smart Client.exe')
    Windows.delay(5)
    Windows.switchToWindowTitle('Login Page')
}

2020-04-28 14:13:48.797 INFO c.k.k.c.w.driver.WindowsDriverFactory - desiredCapabilities = {“appWorkingDir”:“D:\Program Files (x86)\Colt International\Smart Client”}
Apr 28, 2020 2:13:50 PM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
INFO: Detected dialect: OSS
2020-04-28 14:13:56.145 ERROR c.k.k.c.c.keyword.CucumberReporter - :x: User navigate to login page FAILED.
Reason:
org.openqa.selenium.NoSuchWindowException: Currently selected window has been closed (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’
System info: host: ‘EGW-DT-Q10’, ip: ‘10.10.44.250’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_181’
Driver info: io.appium.java_client.windows.WindowsDriver
Capabilities {app: D:\Program Files (x86)\Colt…, appWorkingDir: D:\Program Files (x86)\Colt…, javascriptEnabled: true, platform: WINDOWS, platformName: WINDOWS}
Session ID: 5DB51209-CED5-42E8-BB3C-77F84EA397F6
*** Element info: {Using=name, value=OK}
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.windows.WindowsDriver.execute(WindowsDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.windows.WindowsDriver.findElement(WindowsDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:404)
at io.appium.java_client.DefaultGenericMobileDriver.findElementByName(DefaultGenericMobileDriver.java:118)
at io.appium.java_client.AppiumDriver.findElementByName(AppiumDriver.java:1)
at io.appium.java_client.windows.WindowsDriver.findElementByName(WindowsDriver.java:1)
at io.appium.java_client.MobileDriver$findElementByName.call(Unknown Source)
at steps.LoginSteps.navigate_to_login_page(LoginSteps.groovy:40)