Exception Handling in Java/Selenium Part 1:
Try/Catch Block is one of the
most common Exception handling method in Java/Selenium.
Let's start with an example:
try {
int x = 10;
int y = 0;
int z = x/y;
System.out.println("The value of z is = "+z);
} catch( ArithmeticException e ) {
e.printStackTrace();
}
Output:
java.lang.ArithmeticException: / by zero
OR
try {
int x = 10;
int y = 0;
int z = x/y;
System.out.println("The value of z is = "+z);
} catch( ArithmeticException e ) {
System.out.println("ArithmeticException Occured.");
}
Output:
ArithmeticException Occured.
In try block, we write our executable code which is prone to fail.
And if it fails , you catch (handle) the failure/exception in catch block. A try block
must always be followed by catch block/s or a finally block or both. If no
exception occurs , the execution just skips the catch block. If finally block is
present , in every situation, it goes to the finally block.
Syntax:
try {
executable code ;
} catch( Exception ) {
exception handling code;
}
OR
If there is possibility of more than one types of exception and if you want to handle them individually.
try {
executable code ;
} catch( Exception1 ) {
exception handling code;
} catch( Exception2 ) {
exception handling code;
}
OR
If you want to include finally block also.
If you want to include finally block also.
try {
executable code ;
} catch( Exception ) {
exception handling code;
} finally {
code to conclude the execution.
}
try / catch is used to handle different types of exception(listed below). When more than one types of exception occur, they can be handled at the same
time or individually ( to handle individually , we use multiple catch blocks for each type of
exception). You can also just
ignore the exception and move on. All these depend on how you want to handle
the exception. As explained above, a try block can have more than one catch
blocks.
Most common Exceptions:
ArithmeticException
NullPointerException
ArrayIndexBoundException
IOException
FileNotFoundException
Exception
Let's take example of two very common exceptions : ArithmeticException
and ArrayIndexOutOfBoundsException.
To handle , each of these individually:
try {
executable code ;
} catch( ArithmeticException e) {
e.printStackTrace();
} catch( ArrayIndexOutOfBoundsException e1 ){
e1.printStackTrace();
}
or you may just acknowledge that exception occurred and move on like below:
try {
executable code ;
} catch( ArithmeticException e) {
System.out.println("ArithmeticException occured.");
} catch( ArrayIndexOutOfBoundsException e1 ){
System.out.println("ArrayIndexOutOfBoundsException occured.");
}
It is upto you , how you want to handle the exception in the catch
block.
Exception is the major class of exception which handles all types
of exceptions. So, if you use Exception in catch block, you do not need to write
other catch blocks for individual exceptions. Or if you want to mention the
individual exceptions also along with the major Exception then write the major Exception
at the last catch block only.
try {
executable code ;
} catch (Exception e){
exception handling code;
}
OR
try {
executable code ;
} catch ( ArithmeticException e) {
exception handling code;
} catch ( NullPointerException e1) {
} catch ( NullPointerException e1) {
exception handling code;
}
.
.
.
} catch ( Exception e ){
exception handling code;
}
finally block
finally blocks is optional and if used, comes after catch block. If finally block is used, whether the exception occurs or not , the execution must go through the finally block.
For example:
1. when exception occurs:
try {
int x = 10;
int y = 0;
int z = x/y;
System.out.println("The value of z is = "+z);
} catch( Exception e ) {
System.out.println("Exception Occured");
} finally {
System.out.println("Execution completed");
}
Output:
Exception Occured
Execution completed
2. when exception does not occur:
try {
int x = 10;
int y = 2;
int z = x/y;
System.out.println("The value of z is = "+z);
} catch( Exception e ) {
System.out.println("Exception Occured");
} finally {
System.out.println("Execution completed");
}
Output:
The value of z is = 5
Execution completed
finally block
finally blocks is optional and if used, comes after catch block. If finally block is used, whether the exception occurs or not , the execution must go through the finally block.
For example:
1. when exception occurs:
try {
int x = 10;
int y = 0;
int z = x/y;
System.out.println("The value of z is = "+z);
} catch( Exception e ) {
System.out.println("Exception Occured");
} finally {
System.out.println("Execution completed");
}
Output:
Exception Occured
Execution completed
2. when exception does not occur:
try {
int x = 10;
int y = 2;
int z = x/y;
System.out.println("The value of z is = "+z);
} catch( Exception e ) {
System.out.println("Exception Occured");
} finally {
System.out.println("Execution completed");
}
Output:
The value of z is = 5
Execution completed
No comments:
Post a Comment