CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2007
    Posts
    13

    Exception Catching

    What is the most correct way to throw and catch an exception?

    Code:
    class Exception; // Has a getMessage() method and a few constructors.
    
    void doSomething(void)
    {
    	throw new Exception("You're screwed.");
    }
    
    #include <iostream>
    
    void main(void)
    {
    	try
    	{
    		doSomething();
    	}
    	catch (Exception *e)
    	{
    		std::cerr << e->getMessage() << std::endl;
    	}
    }
    Is that good enough? (I think it is the best method because you can delete your exception after you've gotten what you need. Correct me if I'm wrong.)

    Or should I perhaps throw it like this:
    Code:
    throw Exception("You're screwed.");
    And catch it either by reference:
    Code:
    catch (Exception &e)
    Or by value:
    Code:
    catch (Exception e)
    I'd like to hear your opinions.

  2. #2
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Exception Catching

    Hi.

    If you throw an exception using new you have to deal with memory management. How can you make sure someone is going to deallocate that memory (even yourself didn't do it )? One good way to go is to throw by value (and make sure you don't have code that might throw another exception in your copy constructor). There 're also possiblities of throwing a smart pointer, but you should think whether or not it's really worth it.

    When you catch a reference, you should do it by reference. So, you preserve the polymorphism. Below, it is how I would do (your code with those refactorings).

    Code:
    void doSomething(void)
    {
      throw Exception("You're screwed.");
    }
    
    void main(void)
    {
      try
      {
        doSomething();
      }
      catch (Exception& e)
      {
        std::cerr << e.getMessage() << std::endl;
      }
    }
    You can find nice tips about that (and other things) in the book "C++ Coding Standards".
    Last edited by ltcmelo; April 16th, 2007 at 04:52 PM.

  3. #3
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: Exception Catching

    throw and catch by reference. Using the "new" operator is unnecessary and wasteful/dangerous. Also, you should catch specific exception types and not a "catch all." In this case, you are creating a new Exception class so maybe it's ok for you.
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Exception Catching

    C++ Faq Lite on the subject.

    - petter

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Exception Catching

    ALWAYS

    1) Throw by VALUE
    2) Catch by Reference

    Anything else is simply asking for problems. [e.g. Memory Leaks, Object Slicing]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Feb 2007
    Posts
    13

    Re: Exception Catching

    Thanks to all of you!

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