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



  • 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:

  1. try
  2. catch
  3. throw
  4. throws
  5. 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.


Output :

  • 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 :

Output :


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 :


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.



All these concepts about exceptions and ways of handling them will helps you in your webdriver test exception handling.