|
|
Home J2EE Concepts JMS Java Language Contact Back Next Bookmark this Page |
Some useful tips related to:
|
Exception Handling Overview!In simple words, an exception in program refers to an error in real world. Theoretical definition: "An event during program execution that prevents the program from continuing normally". When an exception occurs, JVM creates an object representing that exception and tries to hand it over to some layer that is written to handle the exception. If no handler is available, then JVM handles the exception in its own way; by printing a small note about the exception, followed by program termination. All exception types are subclasses of Throwable, which is a built-in class. 2 immediate children of Throwable are Exception & Error. Exception refers to those situations, that should be handled by the program; whereas Error should typically be left for JVM to be handled. If you suspect any segment of code that could throw exception, then you should wrap that in try block. Assosciated with try block, is a catch block that contains code to handle the exception (i.e. take necessary steps in case the exception occurs). There may be a finally block; it would contain code that must be executed regardless of whether the exception occurs or not. If a method does not want to handle any particular exception, then it must use Throws clause in its signature. Throws, indicates the exception that could be thrown withing the message; and the code calling this method, must handle the exception mentioned in throws clause; or pass it on to its parent method. However, most of the times there is no point in handling those exceptions that can not be handled by program automatically. Examples of such exceptions include system failure, network outage etc. The only handling that might be relevant here is to catch these exceptions and pass information to a screen that mentions something like "Our server is experiencing issues, please try again.". Detailed exception handling should be used to handle business scenarios. |
|
|
|