Java & J2EE Page 2 - Exception Handling Techniques in Java |
On the previous page we mentioned that exception handling either deals with the exception in the code snippet where it occurs with the try/catch block, or throws the exception back to the runtime engine, which on its end will search for the exception handling routine that is the closest to the place where the exception occurred. It searches within the call stack that contains the sequence of method calls. Generally, exceptions can happen due to either having an abnormal event that leads to an exceptional case, or another method has thrown the exception and now it's time to deal with it, or in the case of an asynchronous exception (which happens only when multiple threads are used and they aren't synchronized appropriately). Now we are going to cover the basics of exception handling: how to "catch" and deal with them. Java allows us to create our own Exception objects and classes-but there is a critical requirement. They must be extending the Exception class, and that's how inheritance happens. It is part of the coding standard that exceptions must be named quite "thoroughly," meaning their name should speak for themselves. throw new Exception(" This is an exception! ");
Now let's see how to catch and deal with an exception. Check out the following.
try { // this is the block of code where the exception happens // sometimes called as source/root of exception // or even called as tricky block or tricky method }
catch (Exception_Type1 e){ // dealing with this kind of exception } catch (Exception_Type2 e){ // dealing with this kind of exception } // ... unlimited number of catches are possible finally { // this block of code is always executed }
The first part of the try-catch-finally construct is the try block. This is the segment where the exception may occur. Generally it's advised to write the minimum amount of code lines here since they are executed only until the exception happens. When it does, the execution jumps to the catch blocks, where the exception types are compared. If they match, then the exception that occurred is dealt with. The finally block is always executed, regardless of whether or not an exception happens during the try block, or whether an exception could be handled within the catch blocks. Since it always gets executed, it is recommended that you do some cleanup here. Therefore, as expected, implementing the finally block is optional. The structure of try-catch blocks is similar to that of switch-case constructs. It should also be said that, in the case of checked exceptions, which must be dealt with, it is possible to either "handle" them right where they occur inside that same method, or throw them further. The latter can be done with the throws keyword. And in this case, the type of exception must be specified in the method signature. See the example: void myMethod () throws SomeKindOfException{ // method goes here }
On the next page we're going to present some more applicable examples.
blog comments powered by Disqus |
|
|
|
|
|
|
|