Java
  Home arrow Java arrow Page 2 - Exception Handling Techniques in Java
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
JAVA

Exception Handling Techniques in Java
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2009-03-11


    Table of Contents:
  • Exception Handling Techniques in Java
  • Exception Handling
  • More Exception Handling Examples
  • Final Thoughts

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Exception Handling Techniques in Java - Exception Handling
    ( Page 2 of 4 )

     

    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. 



     
     
    >>> More Java Articles          >>> More By Barzan "Tony" Antal
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek