Java - Exceptions
When Java interpreter executes a program and encounters an error or exception, it usually stops and throws error message. Handling such situation is termed as error handling or exception handling and Java has three keywords for this purpose:
- try: It is used to test a block of statements for error.
- catch: It has a block of statements which executes when error occurs and handles error.
- finally: It has a block of statements which executes regardless of try-catch blocks result.
try-catch blocks
In the example below, z is undefined, which raises an error. As the error occurred in try block of statement, it will be handled by its catch block of statement. Without try block, the program will stop immediately after an error and throw error message.
Syntax
try{ statements; } catch (Exception e) { statements; }
Example:
public class MyClass { public static void main(String[] args) { try{ int x = 10, y = 0, z; z = x/y; System.out.println(z); } catch (Exception e) { System.out.println("An error occurred."); } } }
The output of the above code will be:
An error occurred.
Many catch blocks
A Java program can have multiple catch blocks to handle each type of exception differently.
Syntax
try{ statements; } catch (ExceptionType_1 e) { statements; } catch (ExceptionType_2 e) { statements; } ... ... } catch (Exception e) { statements; }
Example:
In the example below, z is undefined and raises ArithmeticException. Hence, the respective catch block of code is executed when the error occurs.
public class MyClass { public static void main(String[] args) { try{ int x = 10, y = 0, z; z = x/y; System.out.println(z); } catch (NumberFormatException e) { System.out.println("Number Format error."); } catch (ArithmeticException e) { System.out.println("Arithmetic operation error."); } catch (Exception e) { System.out.println("An error occurred."); } } }
The output of the above code will be:
Arithmetic operation error.
finally block
In the example below, finally block of statement is executed regardless of try-catch block results, which facilitates the program to close the file before proceeding further.
Syntax
try{ statements; } catch (Exception e) { statements; } finally { statements; }
Example:
In the example below, finally block of code is used which is executed after try-catch blocks of code.
public class MyClass { public static void main(String[] args) { try{ int x = 10, y = 0, z; z = x/y; System.out.println(z); } catch (NumberFormatException e) { System.out.println("Number Format error."); } catch (ArithmeticException e) { System.out.println("Arithmetic operation error."); } catch (Exception e) { System.out.println("An error occurred."); } finally { System.out.println("try-catch block finished."); } } }
The output of the above code will be:
Arithmetic operation error. try-catch block finished.
Exception Name & 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. |