Java Exception:
- An exception is a program that occurs during the execution of a program which disturbs the normal flow of instructions.
Java Exception handling:
- Exception handling is the mechanism for handling run-time malfunction.
- To prevent the termination of the program, we have to handle such exceptions.
- Exception exception is an exceptional situation, it is a problem that can occur during the execution of the program.
- A bunch of things can cause exceptions, including programmer error, hardware failures, the need to open files, resource exhaustion etc. can not be found.
Exception’s are categorized into 3 category:
Checked Exception:
- Exceptions that can be guessed by the programmer.
- Example: The file that needs to be opened is not found. Exception of this type should be checked in compile time.
Unchecked Exceptions:
- Unchecked exceptions are RuntimeException extends class.
- Unchecked exceptions are ignored at the compiled time.
- Examples: ArithmeticException, NullPointerException, Array index out of bound exception. Unchecked exceptions are checked at runtime
Error:
- Errors are usually ignored in code because you can rarely do anything about an error.
- Example: If the stack overflow occurs, an error will occur. This type of error code is not possible to handle.
Java Exception handling is done using five keywords:
- try
- catch
- throw
- throws
- finally
1. Using try and catch:
- The attempt is used to preserve the block of code in which an exception can occur. This block of code is called a protected area.
- A catch description involves the announcement of the type of combat with Java Express that you are trying to catch.
- If an exception occurs in a protected code, then the catch block that happens after trying is banned, if the exception of that type is listed in the catch block, the exception is handed over to the catch block who handles it.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Exception_Java { public static void main(String args[]) { int a, b, c; try { a = 0; b = 10; c = b / a; System.out.println("This line will not be executed"); } catch (ArithmeticException e) { System.out.println("Divided by zero"); } System.out.println("After exception is handled"); } } |
Output :
1 2 | Divided by zero After exception is handled |
- An exception will be thrown by this program because we are trying to divide a number by zero under the block trying.
- The attempt of program control has been moved out of the block. Thus, “this line will not be executed” line is never parsed by the compiler.
- Thrown exception handles handle handle once in the block is handled, the program continues with the next line of program control program. Thus the “handled after the exception” line is printed.
- Note: Always use the catch block to log on to your Java exception management in the Selenium WebDriver report.
2. Multiple catch blocks:
- An attempt try block can be followed by multiple catch blocks.
- You can have any number of blocks after a single try block.
- If the exception is found in the protected code then the exception list is given to the first catch block.
- If the exception type of exception matches the first catch block, it is caught, if the exception is not given in the next catch block.
- It continues until the exception is caught or all falls through the catch.
Example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Exception_Java { public static void main(String[] args) { try { int arr[] = { 1, 2 }; arr[2] = 3 / 0; } catch (ArithmeticException ae) { System.out.println("divide by zero"); catch (ArrayIndexOutOfBoundsException e) { System.out.println("array index out of bound exception"); } } } |
Output :
1 | divide by zero |
3. The Throws/Throw Keywords:
- If a method does not handle the check exception, then the method must be declared using the keyword throws.
- Throws the keyword appears at the end of the signature of a method.
- You can throw an exception, either a new instant one or an exception that you have caught.
- Throws are used to postpone a check exception to handle and throw an exception is used to clearly apply.
Example :
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.io.*; import java.rmi.RemoteException; public class Throw_Java { public void deposit(double amount) throws RemoteException{ // Method implementation throw new RemoteException(); } } |
4. The Finally Block:
- finally the block is an attempt block or catch block.
- finally the occurrence of an exception, there is always a block of executed code.
- Eventually, using block you can run a clean-type description of what you want to execute, even then what happens in a secure code.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Finally_Java { public static void main(String[] args) { int a[] = new int[2]; System.out.println("out of try"); try { System.out.println("Access invalid element" + a[3]); //above statement will throw ArrayIndexOutOfBoundException } finally { System.out.println("finally is always executed."); } } } |
Output:
1 2 3 4 5 6 7 | out of try finally is always executed. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at Finally_Java.main(Finally_Java.java:7) |
All these concepts about exceptions and ways of handling them will helps you in your webdriver test exception handling.