SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

How To Handle Exception In Java : Tutorials For Selenium WebDriver

Anyone don't like exceptions and at the same time, anyone can not hide himself from exceptions when running webdriver software automation tests because (What Is An Exception? :->) exception Is an error event generated during the execution of webdriver test case or java software program which disrupts test execution In between. Exception can arise due to the many reasons like, network connection or hardware failure, Invalid data entered by user, DB server down etc.. So all these things can happens any time when you run your selenium webdriver test case and we can not stop It. So Instead of thinking about stopping exception(Which Is not possible) during run time, Think about handling exceptions. Java software development language provides very good exception handling mechanism to recover from this kind of errors. Let us learn different ways of handling exception In java.

There are two types of exceptions In java as bellow.
1. Checked Exception :
Checked exceptions are those exceptions which are checked during compile time and needs catch block to caught that exception during compilation. If compiler will not find catch block then It will throw compilation error. Very simple example of checked exception Is using Thread.sleep(5000); statement In your java software program code. If you will not put this statement Inside try catch block then It will not allow you to compile your java software program.

2. Unchecked Exceptions :
Unchecked exception are those exception which are not checked during compile time. Generally checked exceptions occurred due to the error In code during run time. Simplest example of unchecked exception Is int i = 4/0;. This statement will throws  / by zero exception during run time of java software program.

Handling exceptions using try-catch block
We need to place try catch block around that code which might generate exception. In bellow given example, System.out.println(a[9]); Is written Intentionally to generate an exception. If you see, that statement Is written Inside try block so If that statement throw an exception - catch block can caught and handle It.

public class Handle_exce {

 public static void main(String[] args) {
  int a[] = {3,1,6};
  try { //If any exception arise Inside this try block, Control will goes to catch block.
                         System.out.println("Before Exception"); 
                         //unchecked exception         
                         System.out.println(a[9]);//Exception will arise here because we have only 3 values In array.
                         System.out.println("After Exception");
  }catch(Exception e){
   System.out.println("Exception Is "+e);   
  }
               System.out.println("Outside The try catch.");
 }
}

If you will run above example In your eclipse, try block will be executed. "Before Exception" statement will be printed In console and Next statement will generate an exception so "After Exception" statement will be not printed In console and control will goes to catch block to handle that exception.

Always use try catch block to log your exception In selenium webdriver reports.

Handling exceptions using throws keyword
Another way of handling exception Is using throws keyword with method as shown In bellow given example. Supposing you have a throwexc method which Is throwing some exception and this method Is called from some other method catchexc. Now you wants to handle exception of throwexc method In to catchexc method then you need to use throws keyword with throwexc method.

public class Handle_exce { 
 
 public static void main(String[] args) {
  catchexc();
 }
 private static void catchexc() {
     try {
      //throwexc() Method called.
      throwexc();
     } catch (ArithmeticException e) { //Exception of throwexc() will be caught here and take required action.
         System.out.println("Devide by 0 error.");
     }
 } 
 //This method will throw ArithmeticException divide by 0.
 private static void throwexc() throws ArithmeticException {  
     int i=15/0;     
 } 
}

In above given example, Exception of throwexc() Is handled Inside catchexc() method.

Using throw Keyword To Throw An Exception Explicitly
Difference between throw and throws In java :
As we learnt, throws keyword Is useful to throw those exceptions to calling methods which are not handled Inside called methods. Throw keyword has a different work - throw keyword Is useful to throw an exception explicitly whenever required. This Is the difference between throw and throws In java. Interviewer can ask this question. Let us look at practical example.

public class Handle_exce { 
 
 public static void main(String[] args) {
  catchexc();
 }
 private static void catchexc() {
     try {
      //throwexc() Method called.
      throwexc();
     } catch (ArrayIndexOutOfBoundsException e) { 
         System.out.println("Array Index out of bound exception.");
     }
 } 
 
 private static void throwexc() {
    //This statement will throw ArrayIndexOutOfBoundsException exception.
    throw new ArrayIndexOutOfBoundsException();   
 } 
}

In above example, ArrayIndexOutOfBoundsException exception will be thrown by throw keyword of throwexc method.

finally keyword and Its use
finally keyword Is used with try catch block at the end of try catch block. Code written Inside finally block will be executed always regardless of exception Is occurred or not. Main intention of using finally with try catch block Is : If you wants to perform some action without considering exception occurred or not. finally block will be executed In both the cases. Let us see simple example of finally.

public class Handle_exce { 
 
 public static void main(String[] args) {
  try{
   int i=5/0; //Exception will be thrown.
   System.out.println("Value Of i Is "+i);//This statement will be not executed.
  }catch (Exception e)//Exception will be caught. 
  {
   System.out.println("Inside catch."+e);//print the exception.
  }finally//finally block will be executed.
  {
   System.out.println("Inside finally. Please take appropriate action");
  }
  
  try{
   int j=5/2; //Exception will be not thrown.
   System.out.println("Value Of j Is "+j);//This statement will be executed.
  }catch (Exception e)//No exception so catch block code will not execute.
  {
   System.out.println("Inside catch."+e);
  }finally//finally block code will be executed.
  {
   System.out.println("Inside finally. Please take appropriate action");
  }
 } 
}

In above example, 2 try-catch-finally blocks used. Run above example and observe result.

1st try block will throw an error and catch will handle and then finally block will be executed. 2nd  try block will not throw any error so catch will be not executed but finally block will be executed. So In both the cases finally block will be executed.

So all these things about exception and ways of handling them will helps you In your webdriver test exception handling.

5 comments:

  1. u r god boss!!!!!!!!!!!!!!! thanks a lot

    ReplyDelete
    Replies
    1. Pls don't use this king of words for me. I am just god made man like you.

      Delete
  2. Hi, I am trying to execute a code in my project and at times it throws an unexpected error message (this error message is not because of the code. The application goes down and throws error message). I want to capture the error message and close my browser. Can you please help me how to handle this.

    Warm Regards,
    B Arun

    ReplyDelete
  3. change integer to float or double-> in finally keyword and its use then still it should be give answer 2.5 is not true? Pleas help me.

    ReplyDelete