Exception-Handling Fundamentals


What is an Exception?

An exception is a condition that arises during a code sequence throughout a run. In alternative words, an exception could be a programming error. In pc languages ​​that don't support exception handling, errors should be handled manually - sometimes by mistreatment error codes. This approach is complicated because it is worrying. Java exception handling avoids these issues and brings programming error management to the method in an object-oriented world.


Exception-Handling Fundamentals

A Java exception is an object that describes an exceptional situation that occurred in a piece of code. When an exceptional condition arises, an exception is made and discarded in a manner that represents the one that caused the error. This method can handle the exception itself or pass it. Either way, at one stage the exception is caught and processed. Exceptions may be generated by the Java run-time system or may be generated manually by your code. Exceptions thrown by Java are related to fundamental errors that violate Java language policies or the limitations of the Java editing environment. Manually generated exceptions are usually used to report certain error conditions in a method caller.

Java exception handling is handled with five keywords: try, catch, throw, throws, and finally. In a nutshell, here is a try block detailing the exceptions you want to observe how they work. If there is an exception within the try block, it is thrown. Your code can catch this exception and handle it in some reasonable way. System-generated exceptions are automatically discarded by the Java run-time system. To manually throw an exception, use the keyword throw. Any exceptions thrown out of any procedure such as throws must be specified by the clause. The code that must be executed after an attempted block is completed will finally be blocked.


This is the simplest form of an exception-handling block :

Program
try{
 // block of code to monitor for errors
}
catch(ExceptionType1 obj){
 // exception handler for ExceptionType1
}
catch(ExceptionType2 obj){
 // exception handler for ExceptionType2
}
finally{
 // block of code to be executed after try block ends
}

Here, ExceptionType is the type of exception that has occurred.


Exception Types

All exceptions are the subclass of the built-in class throwable. Thus, throwable exceptions are at the top of the classification. Immediately below the Throwable are two subclasses that differ in two distinct branches. One branch is leading the exception. This category is used for exceptional situations where user programs should "catch". It is also a class that you will subclass to create your own custom exception types. There is an important subclass of exceptions, called "RunTimException". These types of exceptions are automatically defined for the programs you write, including categories by zero and invalid array indicators.

The other branch is topped by Error, which defines the exceptions that your program cannot be expected to catch under normal circumstances. Exceptions to the type of error caused by the Java run-time system itself indicate errors related to the run-time environment.

The top-level exception hierarchy is shown here :

top level exception hierarchy