Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to handle many more exceptions

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to handle many more exceptions

    Hi all,
    I have some piece of code. In that there are chances to get many number of exceptions. My doubt is, to handle all those exceptions do i have to write catch blocks for each type of exception. Is it an efficient way or not. Except using throws keyword, If any other solutions are there please suggest me to do that. Any response will be appreciated.
    Thanks in advance


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to handle many more exceptions

    What you could do is put exception blocks for "special" exceptions, and then a generic exception block which catches Exceptions (the base class of all exceptions). Also, you can put multiple "catch" blocks with one try block.

    try
    {
        // do some stuff
    }
    catch(NullPointerException e)
    {
        // stuff to handle null pointers specially
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
        // stuff to handle array index out of bounds specially
    }
    catch(Exception e)
    {
        // catch and handle all remaining exceptions
    }

Similar Threads

  1. Prepared Statement exceptions please help
    By nrao in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 08:16 PM
  2. How To add my own Exceptions
    By Newtojava in forum Object Oriented Programming
    Replies: 1
    Last Post: September 2nd, 2010, 08:43 AM
  3. El Gamal signature -Handle big integer
    By miotvsingtel in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 9th, 2010, 01:05 PM
  4. Java Exceptions
    By Vinceisg0d in forum Java Theory & Questions
    Replies: 2
    Last Post: March 13th, 2010, 12:25 AM
  5. How to handle quadratic roots that don't exist?
    By kairojya in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2009, 01:21 PM