Thread: When would you choose to return an error code rather than throw an exception?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    When would you choose to return an error code rather than throw an exception?

    Could anybody tell me where to find the answers to the following questions:

    Explain what happens when an exception is thrown in C++.

    What happens if an exception is throws from an object's constructor?

    What happens if an exception is throws from an object's destructor?

    When would you choose to return an error code rather than throw an exception?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    this sounds suspiciously like homework, so I'm not going to give you the answers directly, but since there's an abundance of bad info out there....

    go to google. search for "C++ FAQ lite"
    Last edited by ChaosEngine; 06-06-2007 at 10:44 PM. Reason: damn it, dave! you beat me to it! :)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thank you, But they are NOT homeworks.

    I came across the code:

    Code:
    class A {
    		public:
    			A() {
                                 cout << "A::A()" << endl;
                            }
    			~A() {
                                 cout << "A::~A()" << endl; throw "A::exception";
                            }
    		};
    
    		class B {
    		public:
    			B() {
                                    cout << "B::B()" << endl; throw "B::exception";
                            }
    			~B() {
                                    cout << "B::~B()";
                            }
    		};
    
    
    int main(){
    			try {
    				cout << "Entering try...catch block" << endl;
    
    				A	objectA;
    				B	objectB;
    
    				cout << "Exiting try...catch block" << endl;
    			} 
    			catch (char* ex) {
    				cout << ex << endl;
    return0;
    }
    When I run it using VS 2005, it crashes! Anybody knows why?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    catch( const char* ex)
    maybe?

    and add catch (...) afterwards for checking purposes
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM