Java: When not to use Exceptions.

Discussion in 'Java' started by Sanskruti, May 6, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    The exception handling mechanism is a cleaner way to impose check upon the conditions which may lead to errors. But you should not be carried away by it and start using exception handling anywhere and everywhere you want. Exceptions must be carefully planned and implemented. The following cases suggest when you should avoid the usage of exceptions.

    If you can avoid an erroneous condition without exception handling, easily with a minimum coding, you should prefer to use that than the exception. Though exception handling seems to be very convenient to the programmer, it takes up a lot of time of the processor. For instance, the ArrayIndexOutOfBoundsException is used to detect whether the upper bound of the array has been crossed. If we devote a little more time in testing our code and assure that this erroneous condition would never arise in our program, it will prove to be a more beneficial alternative.

    Sometimes exception handling is taken very lightly and is used as a jumping mechanism forgetting the purpose for which exceptions are used. This is not a good programming practice; and it in fact creates confusion and makes code difficult to maintain.

    Also, if a piece of code is throwing some exception it must be handled properly. Catching the exception just for the sake of catching and leaving the catch block empty does not really help. Remember that the exceptions are the warnings given in advance for the upcoming possible errors which should be handled seriously.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice