Java: When not to use Exceptions.
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.
|All times are GMT +5.5. The time now is 17:27.|