Anyone don't similar exceptions as well as at the same time, anyone tin non shroud himself from exceptions when running webdriver software automation tests because (What Is An Exception? :->) exception Is an mistake trial generated during the execution of webdriver attempt instance or coffee software plan which disrupts attempt execution In between. Exception can arise due to the many reasons like, network connectedness or hardware failure, Invalid information entered past times user, DB server downward etc.. So all these things tin happens whatever fourth dimension when yous run your selenium webdriver attempt instance as well as nosotros tin non halt It. So Instead of thinking virtually stopping exception(Which Is non possible) during run time, Think virtually handling exceptions. Java software evolution language provides real skilful exception treatment mechanism to recover from this form of errors. Let us larn dissimilar ways of handling exception In java.
There are 2 types of exceptions In coffee every bit bellow.
1. Checked Exception :
Checked exceptions are those exceptions which are checked during compile fourth dimension as well as needs grab block to caught that exception during compilation. If compiler volition non discovery grab block as well as thus It volition throw compilation error. Very elementary representative of checked exception Is using Thread.sleep(5000); disputation In your coffee software plan code. If yous volition non set this disputation Inside sweat grab block as well as thus It volition non permit yous to compile your java software program.
2. Unchecked Exceptions :
Unchecked exception are those exception which are non checked during compile time. Generally checked exceptions occurred due to the mistake In code during run time. Simplest representative of unchecked exception Is int i = 4/0;. This disputation volition throws / past times null exception during run fourth dimension of coffee software program.
Handling exceptions using try-catch block
We involve to house sweat grab block only about that code which mightiness generate exception. In bellow given example, System.out.println(a[9]); Is written Intentionally to generate an exception. If yous see, that disputation Is written Inside sweat block thus If that disputation throw an exception - grab block tin caught as well as grip It.
public course of study Handle_exce { world static void main(String[] args) { int a[] = {3,1,6}; try { //If whatever exception arise Inside this sweat block, Control volition goes to grab block. System.out.println("Before Exception"); //unchecked exception System.out.println(a[9]);//Exception volition arise hither because nosotros receive got entirely three values In array. System.out.println("After Exception"); }catch(Exception e){ System.out.println("Exception Is "+e); } System.out.println("Outside The sweat catch."); } }
If yous volition run higher upwards representative In your eclipse, sweat block volition live executed. "Before Exception" disputation volition live printed In console as well as Next disputation volition generate an exception so "After Exception" disputation volition live non printed In console as well as command volition goes to grab block to grip that exception.
Always piece of employment sweat grab block to log your exception In selenium webdriver reports.
Handling exceptions using throws keyword
Another means of treatment exception Is using throws keyword alongside method every bit shown In bellow given example. Supposing yous receive got a throwexc method which Is throwing some exception as well as this method Is called from another method catchexc. Now yous wants to grip exception of throwexc method In to catchexc method as well as thus yous involve to use throws keyword with throwexc method.
public course of study Handle_exce { world static void main(String[] args) { catchexc(); } person static void catchexc() { sweat { //throwexc() Method called. throwexc(); } grab (ArithmeticException e) { //Exception of throwexc() volition live caught hither as well as receive got required action. System.out.println("Devide past times 0 error."); } } //This method volition throw ArithmeticException dissever past times 0. person static void throwexc() throws ArithmeticException { int i=15/0; } }
In higher upwards given example, Exception of throwexc() Is handled Inside catchexc() method.
Using throw Keyword To Throw An Exception Explicitly
Difference between throw as well as throws In coffee :
As nosotros learnt, throws keyword Is useful to throw those exceptions to calling methods which are non handled Inside called methods. Throw keyword has a dissimilar piece of employment - throw keyword Is useful to throw an exception explicitly whenever required. This Is the difference betwixt throw as well as throws In java. Interviewer tin inquire this question. Let us await at practical example.
public course of study Handle_exce { world static void main(String[] args) { catchexc(); } person static void catchexc() { sweat { //throwexc() Method called. throwexc(); } grab (ArrayIndexOutOfBoundsException e) { System.out.println("Array Index out of outflow exception."); } } person static void throwexc() { //This disputation volition throw ArrayIndexOutOfBoundsException exception. throw novel ArrayIndexOutOfBoundsException(); } }
In higher upwards example, ArrayIndexOutOfBoundsException exception volition live thrown past times throw keyword of throwexc method.
finally keyword as well as Its use
lastly keyword Is used alongside sweat grab block at the terminate of sweat grab block. Code written Inside lastly block volition live executed e'er regardless of exception Is occurred or not. Main intention of using lastly alongside sweat grab block Is : If yous wants to perform some activeness without considering exception occurred or not. lastly block volition live executed In both the cases. Let us run across elementary representative of finally.
public course of study Handle_exce { world static void main(String[] args) { try{ int i=5/0; //Exception volition live thrown. System.out.println("Value Of i Is "+i);//This disputation volition live non executed. }catch (Exception e)//Exception volition live caught. { System.out.println("Inside catch."+e);//print the exception. }finally//finally block volition live executed. { System.out.println("Inside finally. Please receive got appropriate action"); } try{ int j=5/2; //Exception volition live non thrown. System.out.println("Value Of j Is "+j);//This disputation volition live executed. }catch (Exception e)//No exception thus grab block code volition non execute. { System.out.println("Inside catch."+e); }finally//finally block code volition live executed. { System.out.println("Inside finally. Please receive got appropriate action"); } } }
In higher upwards example, 2 try-catch-finally blocks used. Run higher upwards representative as well as discovery result.
1st sweat block volition throw an mistake as well as grab volition grip as well as and thus lastly block volition live executed. 2d try block volition non throw whatever mistake thus grab volition live non executed but lastly block volition live executed. So In both the cases lastly block volition live executed.
So all these things virtually exception as well as ways of treatment them volition helps yous In your webdriver attempt exception handling.
http://www.software-testing-tutorials-automation.com/