An Exception is a condition in your program which breaks the normal execution of the program. Like other programming languages, in case of an error condition, Java throws an exception object which has information about the error, state of the program etc. In this event, the run-time environment searches for exception-handler for particular type of exception in the code stack trace. If no handlers are found, then the program terminates.
In Java, all Execptions and Errors are sub-classed from java.lang.Throwable parent class.
+----------------+
| Throwable |
+----------------+
/ \
/ \
/ \
+------------+ +------------+
| Error | | Exception |
+------------+ +------------+
/ | \ / \
( Unchecked ) / \
/ | \ \
( Checked ) +-------------------------+
| Run Time Exception |
+-------------------------+
/ | | \
( Unchecked )
There are mainly three types of exceptions in Java.
Checked Exceptions :- These are the conditions that a program may anticipate based on its application and can handle.
e.g. opening a file which doesn't exist; reading a file beyond its end;
These exceptions are handled at compile-time and if are not handled, compilation errors will occur.
Let's take an example where we are using FileReader class to read data from text file. If you look at the documentation of FileReader class, you'll notice that it throws FileNotFound exception if file is not found, FileNotFound is a checked exception as it has Exception as its super-parent class.
If we compile the below code, compilation will fail even though the text file exists.
Let's take an example where we are using FileReader class to read data from text file. If you look at the documentation of FileReader class, you'll notice that it throws FileNotFound exception if file is not found, FileNotFound is a checked exception as it has Exception as its super-parent class.
If we compile the below code, compilation will fail even though the text file exists.
package com.company; import java.io.FileReader; public class Test { public static void main(String[] args) { FileReader fileObj = new FileReader("test.txt"); } }
>> Error java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
There are two ways to handle these errors :-
- Throwing the exception further the stack using throws
We can use the throws to pass on this execption to the caller method. In our case, since we are using throws for main method, exception handling is at the mercy of JVM which is calling our main method.
We can use the throws to pass on this execption to the caller method. In our case, since we are using throws for main method, exception handling is at the mercy of JVM which is calling our main method.
package com.company; import java.io.FileNotFoundException; import java.io.FileReader; public class Test { public static void main(String[] args) throws FileNotFoundException { FileReader fileObj = new FileReader("test.txt"); } }
- Using Try/Catch
Second option is to wrap the code in try/catch and handle the exception in catch clause.
Second option is to wrap the code in try/catch and handle the exception in catch clause.
package com.company; import java.io.FileNotFoundException; import java.io.FileReader; public class Test { public static void main(String[] args) { try { FileReader fileObj = new FileReader("test.txt"); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Unchecked Exceptions :- These are the exceptions which are extended from the Error or RunTimeException class.
Errors are typically internal Java errors and conditions like out of memory. There is very little that can be done for this by programmer.
RunTimeExceptions includes the conditions like OutofBound Array Access, Null Pointer exception etc.
There will be no compilation error for these exceptions and can be handled using try/catch.
RunTimeExceptions includes the conditions like OutofBound Array Access, Null Pointer exception etc.
There will be no compilation error for these exceptions and can be handled using try/catch.
Comments
Post a Comment