Exceptions in Java
Exceptions in Java
Exceptions in Java are events or objects that occur or are created when some errors arise at run-time. If during program execution, Java Interpreter encounters an error or exception, then it usually stops the program and throws an error message. This is known as exception handling and generally makes use of three keywords in Java for handling this situation which are:
try:
It is a block of code that is considered dangerous as it may result in some exceptions.
catch:
It is a block of code that occurs after an exception is thrown and does the handling work like maybe displaying a message describing the exception in easy words.
finally:
It is a block of code that is placed immediately after the try-catch block and is executed regardless of exception generation. It means that code placed inside the finally block executes either when the exception is thrown or not.
Try-Catch Blocks in Java with Exceptions
Try-catch blocks are placed immediately after the other in a program to deal with exceptions. Syntax of such block is:
Syntax of try-catch block in Java:
try{ statements; }catch(Exception e){ statements; }
Example of try-catch block in Java:
//Java program with try-catch block public class TryCatch { public static void main(String[] args) { try{ int dividend=10, divisor=0, quotient; //trying to divide by zero which is not possible and will thus generate exception quotient=dividend/divisor; System.out.println(quotient); }catch(Exception e){ System.out.println("Something went wrong!"); } } }
Output:
Something went wrong!
Many Catch Blocks
While handling exceptions, many catch blocks can be used to handle different types of exceptions differently. Syntax of such blocks is:
Syntax of the try-catch block with many catch blocks in Java:
try{ statements; }catch(Exception1 e1){ statements; }catch(Exception2 e2){ statements; } ... catch(Exception e){ statements; }
Example of try-catch block with multiple catch blocks in Java:
//Java program with try-catch block with multiple catch blocks public class MultipleCatch{ public static void main(String[] args) { try{ int dividend=10, divisor=0, quotient; //trying to divide by zero which is not possible and will thus generate exception quotient=dividend/divisor; System.out.println(quotient); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Trying to access element at invalid index"); }catch(ArithmeticException e){ System.out.println("Trying to divide by zero"); }catch(Exception e){ System.out.println("Something went wrong!"); } } }
Output:
Trying to divide by zero
Finally Block
Finally block is placed after the last catch block and usually consists of file closing or objects destroying statements. It is executed no matter what happens inside the try block.
Syntax of the try-catch block with finally block in Java:
try{ statements; }catch(Exception e){ statements; }finally{ statements; }
Example of the try-catch block with finally block in Java:
//Java program with try-catch block with finally blocks public class FinallyBlock { public static void main(String[] args) { try{ int dividend=10, divisor=0, quotient; //trying to divide by zero which is not possible and will thus generate exception quotient=dividend/divisor; System.out.println(quotient); }catch(Exception e){ System.out.println("Something went wrong!"); }finally{ System.out.println("Will always execute!!"); } } }
Output:
Something went wrong! Will always execute!!
Types of Exceptions in Java
In Java, there are two types of exceptions:
-
-
Built-in Exceptions in Java
-
User-defined Exceptions in Java
-
Built-in Exceptions in Java
They are the pre-defined exceptions available in Java libraries. Examples of such exceptions are:
-
-
- ArithmeticException
- ArrayIndexOutOfBoundsException
- ClassNotFoundException and many more
-
User-defined Exceptions in Java
These are the exceptions that the user creates according to its requirements when built-in exceptions are not enough. They are created by inheriting the Exception class.
Exceptions in Java and their Description
Exception Name | Description |
ArithmeticException | Raised for incorrect arithmetic operation. |
ArrayIndexOutOfBoundsException | Raised when array index is either negative or greater than or equal to the size of the array. |
ClassNotFoundException | Raised when class definition is not found. |
FileNotFoundException | Raised when required file is not found. |
IllegalArgumentException | Raised when incorrect argument is passed into a method |
InterruptedException | Raised when a thread is waiting, sleeping or processing and it is interrupted |
IOException | Raised when input/output operation failed or interrupted. |
NoSuchFieldException | Raised when a class does not contain specified field |
NoSuchMethodException | Raised when a class does not contain specified method |
NullPointerException | Raised when referring to the member of a null object. |
NumberFormatException | Raised when a string could not be converted into numeric format. |
RuntimeException | Raised exception occurs during run time. |
SQLException | Raised when error occurred while executing SQL queries on database |
StringIndexOutOfBoundsException | Raised when string index is either negative or greater than or equal to the size of the string. |