// By:Team // No Comment
Exception are used in Java to handle errors or any other exceptional event that occurs in the normal flow of program. There are several way Exception can occur in Java.
- Data provided is not in expected format(eg. int instead of String).
- DB can not connected.
- Network connection Lost.
- Object is null.
Java Exception Hierarchy
Every Exception in Java is sub type of Exception class which in turn is the subclass of Throwable. And as we know everything in Java derived from Object class Throwable also derived from class Object. Exception and Error are two different classes that derived from Throwable. Errors represent situation which doesn’t occur because of Programming error but that might happen at the time of program execution and these are abnormal behavior which java program can not handle and shouldn’t bother to handle. JVM running out of memory is a type of Error which can occur at runtime.
Checked Vs Unchecked Exception
- Checked exceptions are subclass’s of Exception excluding RuntimeException and its subclasses.
- Checked Exceptions force programmers to deal with the exception that may be thrown.
- When a checked exception occurs in a method, the method must either catch the exception and take the appropriate action,or pass the exception on to its caller.
Example of Checked exception
- Unchecked exceptions are RuntimeException and any of its subclasses.
- Compiler doesn’t force the programmers to either catch unchecked exception or declare it in a throws clause.
- Programmers may not even know that the exception could be thrown.
- Checked exceptions must be caught at compile time.
- Runtime exceptions do not need to be.
Example of Unchecked Excpetion
Java Exception Handling
Now we know that exception can occur in Java programme at any time(or Any Location). So we need to know how to handle these exceptions. Handling exception is required attribute in developing robust application. We can handle exception by