Amar
Amar

Reputation: 1

Selenium - Throwable does not catch exception

I am trying to use throwable class in my code given below & for some reason its not catching an exception in particular scenario. My test case is to verify a particular text on google search page after Safe Search mode is On. So basically i am matching the text 'SafeSearch on'(which we normally see next to the settings button on the google page if Safe Search is ON) with the Xpath of that location.I am using assertion. My test case executes fine when the 'SafeSearch on' text is there on the system & when there is mismatch between actual & expected string values. However when the text is not listed then after execution the Catch block does no catch the exception. My Code gets Stuck when it tries to find text using xpath. Once i manually close the browser it then gets me the message 'Text Not Matched - Null'. I am expecting the browser to close & in output i should get the Null exception caught. Can anybody help me in this scenario? Here is my code snippet

String S1 = "//*[@id='ab_ctls']/li[1]/span";
String S2 = "SafeSearch On";
System.out.println("Test before search");
try {
Assert.assertEquals(d1.findElement(By.xpath(S1)).getText(),S2);
System.out.println("Text Matched");
} catch (Throwable e){
System.out.println("Text not Matched"+"---"+ e.getmessages());
} 
d1.quit();

Upvotes: 0

Views: 579

Answers (1)

Ajinkya
Ajinkya

Reputation: 22720

Throwable will catch everything. You should catch exception instead of throwable because with throwable you are catching exception plus erros.

try{
//....
}catch (Exception e){
// Do Something
}

Exception hierarchy in Java

enter image description here

This will help Why catch Exceptions in Java, when you can catch Throwables?

Upvotes: 1

Related Questions