
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Exception
Java Exception is a runtime error that occurs during the execution of a program. It disrupts the normal flow of the program and is usually caused by the mistakes in the code. Java Exception is divided into two categories, checked and unchecked exceptions. The checked exceptions are checked at compile-time, while the unchecked exceptions are checked at runtime.
Types of Java Exceptions
There are two types of exceptions in Java:
- Checked Exception: The checked exceptions are checked at compile-time. It means if a method is throwing a checked exception, then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error.
- Unchecked Exception: The unchecked exceptions are not checked at compile time. It means if your program is throwing an unchecked exception and even if you didn't handle/declare that exception, the program won't give a compilation error. The unchecked exceptions are also called as runtime exceptions.
Java Exception Hierarchy
Java Exception class is the parent class of all exceptions in Java. If you catch an Exception object, you can be sure that you catch all the exceptions. The Java Exception class has many subclasses that represent the different types of exceptions.
Here is a hierarchy of Java Exception classes:
- Throwable: Throwable is the parent class of Java Exceptions hierarchy and it has two child objects, Error and Exception. The Throwable class has two subclasses:
- Error: The errors are exceptional scenarios that are out of the scope of application and it's not possible to anticipate and recover from them, for example, hardware failure, JVM crash, etc.
- Exception: The exceptions are the exceptional scenarios that can be caught and recovered from, for example, FileNotFoundException, SQLException, etc.
Java Exception Keywords
There are five keywords used to handle exceptions in Java:
- try: The try block is used to enclose the code that might throw an exception.
- catch: The catch block is used to handle the exception. It must be used after the try block only.
- finally: The finally block is used to execute the important code of the program. It is executed whether an exception is handled or not.
- throw: The throw keyword is used to throw an exception.
- throws: The throws keyword is used to declare an exception.
Java Exception Example
In this example, we are dividing a number by zero, so JVM will throw an ArithmeticException.
public class ExceptionExample { public static void main(String[] args) { int a = 10; int b = 0; int c = a/b; System.out.println(c); } }
Output
Following is the output of the above code:
Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionExample.main(ExceptionExample.java:5)
As you can see in the output, JVM throws an ArithmeticException because we are dividing a number by zero.
Handling Java Exception
In this example, we are dividing a number by zero, so JVM will throw an ArithmeticException. We are handling this exception using try-catch block.
public class ExceptionExample { public static void main(String[] args) { int a = 10; int b = 0; try { int c = a/b; System.out.println(c); } catch(ArithmeticException e) { System.out.println("Can't divide a number by zero"); } } }
Output
Following is the output of the above code:
Can't divide a number by zero
As you can see in the output, we are handling the ArithmeticException using try-catch block.
Java Exception Handling Example
In this example, we are handling the checked exception using try-catch block.
import java.io.*; public class ExceptionExample { public static void main(String[] args) { try { FileReader file = new FileReader("C:\\test\\a.txt"); BufferedReader fileInput = new BufferedReader(file); for (int counter = 0; counter < 3; counter++) System.out.println(fileInput.readLine()); fileInput.close(); } catch (IOException e) { System.out.println("File not found"); } } }
Output
Following is the output of the above code:
File not found
As you can see in the output, we are handling the checked exception using try-catch block.
Java Exception Handling Example with Multiple Catch Blocks
In this example, we are handling the checked exception using multiple catch blocks.
import java.io.*; public class ExceptionExample { public static void main(String[] args) { try { FileReader file = new FileReader("C:\\test\\a.txt"); BufferedReader fileInput = new BufferedReader(file); for (int counter = 0; counter < 3; counter++) System.out.println(fileInput.readLine()); fileInput.close(); } catch (FileNotFoundException e) { System.out.println("File not found"); } catch (IOException e) { System.out.println("IO Exception occurred"); } } }
Output
Following is the output of the above code:
File not found
As you can see in the output, we are handling the checked exception using multiple catch blocks.
Java Exception Handling Example with finally block
In this example, we are handling the checked exception using finally block.
import java.io.*; public class ExceptionExample { public static void main(String[] args) { try { FileReader file = new FileReader("C:\\test\\a.txt"); BufferedReader fileInput = new BufferedReader(file); for (int counter = 0; counter < 3; counter++) System.out.println(fileInput.readLine()); fileInput.close(); } catch (FileNotFoundException e) { System.out.println("File not found"); } catch (IOException e) { System.out.println("IO Exception occurred"); } finally { System.out.println("Finally block is always executed"); } } }
Output
Following is the output of the above code:
File not found Finally block is always executed
As you can see in the output, we are handling the checked exception using finally block.
Java Exception Handling Example with throw keyword
In this example, we are throwing an exception using the throw keyword.
public class ExceptionExample { public static void main(String[] args) { try { throw new ArithmeticException("Arithmetic Exception occurred"); } catch(ArithmeticException e) { System.out.println(e.getMessage()); } } }
Output
Following is the output of the above code:
Arithmetic Exception occurred
As you can see in the output, we are throwing an exception using the throw keyword.