CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006
    Posts
    22

    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

  2. #2
    Join Date
    May 2007
    Posts
    35

    Re: handeling exceptions

    Quote 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.

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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
  •  





Click Here to Expand Forum to Full Width

Featured