Monday, 25 March 2013

Exception Handling

    Java has many wonderful things and exception handling is one among them. Exception handling is a framework provided by Java to ensure program execution continues in an unfavorable problem condition by handling it gracefully. Exception handling is not new to Java guys and I need not give a heroic introduction to it. This is my attempt at writing a detailed tutorial like a chapter in a Java book, by giving comprehensive coverage to this topic accompanied by code examples, images, bells and whistles. I will give a simple and easy to understand narrative to java exception handling and try to uncover intricate detail as much as possible in my own style. As exception handling is a large topic, this will be a multi-part series.
I chose to use the word problem instead of error in the above definition. Error is a java class and given a specific meaning in Java exception handling and I didn’t want that to be mixed with the general English word which denotes an unfavorable condition. Google says, problem is “A matter or situation regarded as unwelcome or harmful and needing to be dealt with and overcome”. In general software terms we use error to signify that and they are of two types, compile time and run time. In exception handling we will be dealing only with run-time problems. In any software program, there are numerous possibilities that a problem can occur at run-time during the flow of execution. How it is handled is crucial to the business and user. Right from Ada to C++ to PHP exception handling is available.
Imagine if there is no exception handling mechanism available and what would happen if a run time error occurs. Most likely the program may crash in ugly manner resulting in an un wanted situation. In those situations, we can analyze the programs and use if-condition construct and try to salvage the situation. Repair the code and re-run it. Some problems cannot be handled and will result in reporting error code and abruptly discontinue the execution. All these will result in unorganized / non maintainable code. With Java’s exception handling mechanism we have an organized and simplified more importantly object oriented way of handling run-time problems.

No comments:

Post a Comment