Sunday, December 10, 2017

Exception Handling

Introduction:Exception is any undesired condition or a case which can interrupt normal flow of any application.
During the development of any application regardless of implementing language, there might be some cases in which some special cases can results in unexpected behavior. You may want to handle these cases according to different situations. For example, In some cases you may want to stop execution your application, and in other cases you may want to redirect flow of application or return with some message so that end user knows the problem and can take appropriate action.

Reasons: In an application, there might be multiple reasons for occurring exceptions.
i.g. wrong data format, wrong or invalid data provided by client, hardware failure, network connection failure, Database server down etc.

So, you like or not, exceptions can occurred in any application, you need to deal with it. Fortunately, mostly languages come with exception handling capabilities. Java can handle exceptions in a very effective and robust way. When any exception occurred during execution of your code, JVM stops execution and tries to find appropriate handler registered with application to handle occurred application. If JVM could not find any exception handler, it just kill the program and your application may crash. But if any handler is available to handle occurred exception, JVM create an instance of that exception and make it available to exception handler. This exception object contains very useful information and helps to take proper action.

Exception object has following exception
Method hierarchy
Line number where the exception occurred
Type of exception
Reason of exception and so on...

Catching Exception: Action defined in exception handler to handle particular action is called catching the exception. If any exception occurred, and it is catched, running application will not be terminated unexpectedly by JVM.

Throwing Exception: Instead of catching any exception you as developer have option to delegate responsibility of catching exception to next level. This mechanism is called throwing exception.

Re-throwing Exception: During execution of an application, if any exception occurred and we catch that exception in our code using catch block but instead taking proper action we create new instance of some other type of exception (Usually Costume Exceptions) and throw it manually using throw keyword. That's called re-throwing exception.

How to handle or throw exception, we are going to learn it soon.

As stated earlier, when any exception is raised an exception object is getting created. Java Exceptions are hierarchical and inheritance is used to categorize different types of exceptions. Throwable is the parent class of Java Exceptions Hierarchy and it has two child objects – Error and Exception. Exceptions are further divided into checked exceptions and runtime exception.
Errors: Errors are exceptional scenarios that are out of scope of application and it’s not possible to anticipate and recover from them, for example hardware failure, JVM crash or out of memory error. That’s why we have a separate hierarchy of errors and we should not try to handle these situations. Some of the common Errors are OutOfMemoryError and StackOverflowError.
Checked Exceptions: There are some exceptions that are actually check by compiler at the time of compiling the code. If your code can throw some exceptions that are directly extends Exception class or not extending RuntimeException class or its subclasses, compiler will check these exception at compile time and will force you to catch or re-throw occurred exception. Checked Exceptions can be anticipated in a program and can be recovered, for example FileNotFoundException. We should catch this exception and provide useful message to user and log it properly for debugging purpose.
Unchecked Exceptions or Runtime Exception: Unchecked Exceptions are the exceptions that actually occurred at runtime and can not be anticipated at compile time. If any exception class is extending RuntimeException or it subclass it will be Unchecked Exception. Compiler will not force you to catch or throw it. Its completely up to developer to handle it or to leave for runtime.
Mostly time Runtime Exceptions are cause by bad programming, for example trying to retrieve an element from the Array, or the NullPointerException etc.
Exception Handling in Java – Useful Methods
Throwable is a class at the top of hierarchy of exceptions. It comes up with some set of methods explained below. It has many sub-classes, we have already discussed some of them.
Some of the useful methods of Throwable class are;
public String getMessage() – This method returns the message String of Throwable and the message can be provided while creating the exception through it’s constructor.
public String getLocalizedMessage() – This method is provided so that subclasses can override it to provide locale specific message to the calling program. Throwable class implementation of this method simply use getMessage() method to return the exception message.
public synchronized Throwable getCause() – This method returns the cause of the exception or null id the cause is unknown.
public String toString() – This method returns the information about Throwable in String format, the returned String contains the name of Throwable class and localized message.
public void printStackTrace() – This method prints the stack trace information to the standard error stream, this method is overloaded and we can pass PrintStream or PrintWriter as argument to write the stack trace information to the file or stream.


Post a Comment