Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ catching exception in constructor

How can I protect myself from using object which isn't fully created when using exceptions? Should I catch in constructor ? Or maybe it's bad practice ? If I'll catch in constructor object will be created.

#include <stdio.h>

class A
{
public:
    A()
    {
        try {
            throw "Something bad happened...";
        }
        catch(const char* e) {
            printf("Handled exception: %s\n", s);
        }
        // code continues here so our bad/broken object is created then?
    }
    ~A() 
    { 
        printf("A:~A()"); 
    }

    void Method()
    { // do something
    }
};

void main()
{
    A object; // constructor will throw... and catch, code continues after catch so basically we've got 
              // broken object.

    //And the question here: 
    //
    //* is it possible to check if this object exists without catching it from main? 
    // &object still gives me an address of this broken object so it's created but how can I protect myself 
    // from using this broken object without writing try/catch and using error codes?
    object.Method(); // something really bad. (aborting the program)

};
like image 758
user3841735 Avatar asked Oct 02 '14 20:10

user3841735


People also ask

Can we handle exceptions in constructor?

The short answer to the question “can a constructor throw an exception in Java” is yes!

Can we use try catch in constructor?

Yes, we can write try catch and finally block in a constructor ant it works properly.

Is it safe to throw an exception from a constructor?

Yes, constructors are allowed to throw an exception in Java. A Constructor is a special type of a method that is used to initialize the object and it is used to create an object of a class using the new keyword, where an object is also known as an Instance of a class.


2 Answers

The language itself has no concept of an object being "invalid" in any detectable way.

If the exception indicates that a valid object can't be created, then it shouldn't be handled within the constructor; either rethrow it, or don't catch it in the first place. Then the program will leave the scope of the object being created, and it won't be possible to incorrectly access it.

If that isn't an option for some reason, then you'll need your own way to mark the object as "invalid"; perhaps set a boolean member variable at the end of the constructor to indicate success. This is flaky and error-prone, so don't do it unless you've got a very good reason.

like image 175
Mike Seymour Avatar answered Oct 16 '22 13:10

Mike Seymour


If the object is in an invalid state when a certain exception is thrown, then I would let the exception unwind the call stack so the caller can be notified (and therefore react) to such things.

However, if the exception is one you can recover from, it may be worth trying to do so depend on your application. Make sure you use something like a logger or even simply stderr to indicate this is happening though.

like image 44
OMGtechy Avatar answered Oct 16 '22 15:10

OMGtechy