In the previous tutorial we have learnt about the basics of java exceptions. In this tutorial we are looking one step forward i.e Different Types of Exception and Exception Hierarchy.
Types of Exceptions in Java :
Java Exceptions are divided into 3 types – Checked Exceptions,Unchecked Exceptions and Errors.
In Java, there are multiple situations that can cause exception. These situations are divided into 4 different types -Situation | Cause of Exception |
---|---|
Code Error | Exceptions occurred due to wrong or invalid data. If user try to access the array element greater than size or something divided by zero error then these exceptions are called as code errors or data errors. |
Standard Method Exception | Exceptions may be thrown if we try to access the standard methods with invalid input parameter. |
Own Exception | User may generate his/her own exceptions depending on situation and type of code. |
Java Errors | Errors occurred due to JVM |
A. Checked exceptions :
- A checked exception is an exception which is error or a problem occurred because of code written by programmer
- Checked Exception cannot be neglected by the programmer.
- Suppose we are opening the file and file is not present then exception occurred during the compile time can be considered as checked exception.
- These exceptions cannot be ignored at the time of compilation.
- Class that extend throwable class except RuntimeException and Error is considered as Checked exception.
B. Runtime exceptions:
- Classes that extent RuntimeException class is called as Runtime Exception.
- Runtime exceptions are ignored at the time of compliation.
C. Errors :
- These are not exceptions but the problems that are beyond the control of the user or the programmer.
- For example, Stack overflow, Virtual Machine Memory,AssertionError
Exception Hierarchy :
Image may be NSFW.
Clik here to view.
The post Types of Exception and Exception Hierarchy : Java Programming appeared first on Learn Java Programming.