Click to See Complete Forum and Search --> : handeling exceptions


Autofreak
August 5th, 2008, 02:45 PM
Hi,
I have a general question on exception handling. I instantiated an object and want to call one of its methods. There are five diffrent exceptions documented for this method. The question is Sould I put the method call in the try block and create catch blocks for all five exceptions?

Regards,
serge

Leedogg
August 5th, 2008, 02:52 PM
Hi,
I have a general question on exception handling. I instantiated an object and want to call one of its methods. There are five diffrent exceptions documented for this method. The question is Sould I put the method call in the try block and create catch blocks for all five exceptions?

Regards,
serge


I think this is a style question dependent on how robust you want to make your application. If you do have more than one catch block be sure to order them from most specific to most general in terms of the types of exceptions you are catching.

boudino
August 6th, 2008, 01:44 AM
Generally, it depends on how you want to process exception. If you want specific processing for specific exception (e.g. some are just logged, some are anounced with message box), you need to create separated catch blocks. If you want to catch any exception and process all types of exception in the same way, only one catch block is sufficient. In the case catching, common predecestor is sufficient.

Keep in mind: some method throw several documented specialized expceptions, but also can throw any other exception, which is raised from code outside the method. Than is semantic difference among several catch blocks and one catch block. With one catch block, you catch everything and you cannot distinguish, which exception is documented (you know how to handle it) and which not. In such a case, it could be better to write several catch block for all documented exception types to handle known exceptions correctly, and let all other (undocumented) exceptions continue up the call stact.