Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [Solved] Unhandled exception occurring inside my 'try'-block
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved C++ Gurus
6 Posts 3 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        muhahaa
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            [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;
            

            }
            @

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved