|
-
August 5th, 2008, 02:45 PM
#1
handeling exceptions
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
-
August 5th, 2008, 02:52 PM
#2
Re: handeling exceptions
 Originally Posted by Autofreak
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.
-
August 6th, 2008, 01:44 AM
#3
Re: handeling exceptions
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|