Wednesday, September 12, 2018

Overview of Java Exception Handling



Exception handling in Java provides a way to handle a situation in which an exception is thrown by displaying a meaningful message to the user and continuing (or stopping) the program flow.

If an exception occurs in a method, the method (where the exception occurred) creates an exception object and throws it. The created exception object contains information about the error, its type, and the status of the program when the error occurred.

The method with which the exception is posted can manipulate or propagate this exception. When it is transferred, the runtime system goes through the method hierarchy that was called to access the current method to find a method that can manipulate the exception.

If your program can not detect a specific exception, this is handled by the standard controller. The default handler displays a message describing the exception, prints a batch trace from the point where the exception occurred, and exits the program.

Five keywords used to manage Java exception handling

·   try - Any code that might throw an exception is enclosed within a try block.
· catch - If an exception occurs in try block, catch block can provide exception handlers to handle it in a rational manner.
·  finally - The finally block always executes when the try block exits. So, any code that must execute after a try block is completed should be put in finally block.
·  throw - throw is used to manually thrown an exception.
· throws - Any exception that is thrown in a method but not handled there must be specified in a throws clause.

General form of excpetion handling in Java


try {
  // block of code
}
catch (ExceptionType1 expObj) {
  // exception handler for ExceptionType1
}
catch (ExceptionType2 expObj) {
  // 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 Hierarchy in Java

Throwable class is the super class of all the exception classes in Java. Below Throwable class there are two subclasses which denotes two distinct branches of exceptions -
· Exception - An Exception indicates that a problem has occurred, but it is not a serious system problem. The user programs you write will throw and catch Exceptions.
· Error - It defines exceptions that are not expected to be caught by your program. Exceptions of type Error are used by the Java run-time system to indicate errors having to do with the run-time environment, itself.
Examples of error are StackOverflowError, OutOfMemoryError etc.

Kinds of exception in Java

· Checked Exception - These are exceptional conditions that an application should anticipate and recover from.
· RunTime Excpetion - These are exceptional conditions that are external to the application, and the application usually cannot anticipate or recover from them.
·   Error - It defines exceptions that are not expected to be caught by your program.

Error and runtime exceptions are collectively known as unchecked exceptions.
Please refer to checked exception Vs unchecked exception for detailed explanation of checked and unchecked exceptions.

Advantages of Exception Handling

·                     To maintain the normal flow of the application.
·                     Separation of concerns by separating error-handling code from regular code.
·                     Providing a proper message for the failing condition.
·                     Preventing the program from automatically terminating.




No comments:

Post a Comment

From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9 i...