[Solved] Unhandled exception occurring inside my 'try'-block



  • I'm inside a constructor, and get a reference as a parameter.

    Due to a severe coding error on the caller's side, the reference might be invalid. I'd like to check that and post a helpful error message before the application crashes.

    However, when I access an invalid reference, even inside a try {}, I get an unhandled exception "access violation" (VS 2010)

    Isn't the try block supposed to handle that very exception?



  • What do you understand by 'invalid reference' (there is no such thing as an invalid reference in C++)?



  • The C++ try doesn't catch CPU exceptions, like access violation or division by zero. You can use SetUnhandledExceptionFilter or structured exception handling (__try, __except).

    http://msdn.microsoft.com/en-us/library/ms680657(VS.85).aspx
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms680634(v=vs.85).aspx



  • [quote author="Lukas Geyer" date="1357138135"]What do you understand by 'invalid reference' (there is no such thing as an invalid reference in C++)?[/quote]

    There is such a thing as a reference to an object that has since been deleted, or an object that has not yet been initialized (e.g. because a constructor initializes objects in the wrong order).

    How would you call a reference to an object that does not yet, or no longer exists, if not 'invalid'?

    [quote author="muhahaa" date="1357139468"]The C++ try doesn't catch CPU exceptions, like access violation or division by zero. You can use SetUnhandledExceptionFilter or structured exception handling (__try, __except).

    http://msdn.microsoft.com/en-us/library/ms680657(VS.85).aspx
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms680634(v=vs.85).aspx[/quote]

    Thanks. That squarely answers my question.



  • [quote author="Asperamanca" date="1357143117"]How would you call a reference to an object that does not yet, or no longer exists, if not 'invalid'?[/quote]

    A reference to an invalid object. ;-)

    A "signal handler":http://www.cplusplus.com/reference/csignal/signal/ can be used on non-MSVC platforms.

    On a sidenote: please be aware that there is no way to detect whether an object has been deleted or not yet initialized. You may get an segmentation fault (or exception), or not. The following code does not raise an exception (or signal, it even works as expected), although it is cleary defective.

    @
    class SomeClass
    {
    public:
    void write() { std::cout << "SomeClass::write()"; }
    };

    void write(SomeClass &someObject)
    {
    someObject.write();
    }

    int main(int argc, char *argv[])
    {
    SomeClass *someObject = new SomeClass;
    SomeClass &someObjectReference = *someObject;

    delete someObject;
    
    write(someObjectReference);
    
    return 0;
    

    }
    @



  • [quote author="Lukas Geyer" date="1357201159"]On a sidenote: please be aware that there is no way to detect whether an object has been deleted or not yet initialized. You may get an segmentation fault (or exception), or not.[/quote]

    I know. It's only up to the trash you happen to have or not have in the part of memory that is referenced. However, I hoped that checking for a certain, excepted value would give me a high likelihood of catching errors. Certainly not 100 %, to be sure.

    The application crashes anyway, however I found that with a destroyed stack, it's really hard to find the cause of the problem. If at least in 80 % of the cases, I can write a meaningful logging before crashing, that helps a lot.

    Edit:
    Come to think of it...if I changed a key signature member variable in an object's destructor, I should be able to detect deleted references. Also not 100 %, but it might increase the likelihood.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.