Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. try catch not working

try catch not working

Scheduled Pinned Locked Moved C / C++ / MFC
saleshelp
6 Posts 3 Posters 0 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.
  • B Offline
    B Offline
    Bangerman
    wrote on last edited by
    #1

    My very large app was shipped to a customer and as you might guess it failed. My code has lots of try catches in it and I though it strange that it failed with no exception traps shown in the log. After trying to insert some deliberate exceptions I found that the exception traps are not working at all. To test what was going on I created a console app: int _tmain(int argc, _TCHAR* argv[]) { printf("Start\n"); try { int *pWord=NULL; *pWord=999; } catch(...) { printf("Error was caught\n"); } printf("Stop\n"); return 0; } This should display Start Error was caught Stop But it actually displays Start .. the the MS crash dialog pops up. Does anyone know why my exception traps have been disabled. Richard.


    Hell I thought it was funny .....

    S Z 2 Replies Last reply
    0
    • B Bangerman

      My very large app was shipped to a customer and as you might guess it failed. My code has lots of try catches in it and I though it strange that it failed with no exception traps shown in the log. After trying to insert some deliberate exceptions I found that the exception traps are not working at all. To test what was going on I created a console app: int _tmain(int argc, _TCHAR* argv[]) { printf("Start\n"); try { int *pWord=NULL; *pWord=999; } catch(...) { printf("Error was caught\n"); } printf("Stop\n"); return 0; } This should display Start Error was caught Stop But it actually displays Start .. the the MS crash dialog pops up. Does anyone know why my exception traps have been disabled. Richard.


      Hell I thought it was funny .....

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      All is working as it should on a standard compliant C++ compiler. Low level exceptions are not meant to be caught by C++ catch blocks; only C++ exceptions thrown by the C++ throw keyword are. MSVC6 was buggy in this area and such exceptions could be caught by this construct. This bug was fixed in later versions. The "old" (non standard) behaviour can be enabled via the /EHa[^] compiler switch. Alternatively you can use SEH[^] to catch low level exceptions; be warned however that this is specific to the Windows platform and doesn't unwind the stack.

      Steve

      B 2 Replies Last reply
      0
      • S Stephen Hewitt

        All is working as it should on a standard compliant C++ compiler. Low level exceptions are not meant to be caught by C++ catch blocks; only C++ exceptions thrown by the C++ throw keyword are. MSVC6 was buggy in this area and such exceptions could be caught by this construct. This bug was fixed in later versions. The "old" (non standard) behaviour can be enabled via the /EHa[^] compiler switch. Alternatively you can use SEH[^] to catch low level exceptions; be warned however that this is specific to the Windows platform and doesn't unwind the stack.

        Steve

        B Offline
        B Offline
        Bangerman
        wrote on last edited by
        #3

        Thank you, that explains why it all worked fine under the old compiler. Your reply is much appreciated.


        Hell I thought it was funny .....

        1 Reply Last reply
        0
        • S Stephen Hewitt

          All is working as it should on a standard compliant C++ compiler. Low level exceptions are not meant to be caught by C++ catch blocks; only C++ exceptions thrown by the C++ throw keyword are. MSVC6 was buggy in this area and such exceptions could be caught by this construct. This bug was fixed in later versions. The "old" (non standard) behaviour can be enabled via the /EHa[^] compiler switch. Alternatively you can use SEH[^] to catch low level exceptions; be warned however that this is specific to the Windows platform and doesn't unwind the stack.

          Steve

          B Offline
          B Offline
          Bangerman
          wrote on last edited by
          #4

          So I made the changes and it made no difference to the problem. However, I managed to connect the debugger and what I've found is frightening. The problem is ocurring because I'm passing an invalid parameter to localtime_s but rather than reporting any error or throwing an exception it is directly calling: _invoke_watson(pszExpression, pszFunction, pszFile, nLine, pReserved); So msvcrt80.dll directly invokes DrWatson rather than throwing an error for you to catch. Now looking at their code I can see that in order to catch invald parameters in my code I'm going to have to add an invalid parameter handler into it. This seems strange to me, surly rather than calling DrWatson, it should throw an exception or even return the error that the docs say it will return for an invalid value.


          Hell I thought it was funny .....

          S 1 Reply Last reply
          0
          • B Bangerman

            My very large app was shipped to a customer and as you might guess it failed. My code has lots of try catches in it and I though it strange that it failed with no exception traps shown in the log. After trying to insert some deliberate exceptions I found that the exception traps are not working at all. To test what was going on I created a console app: int _tmain(int argc, _TCHAR* argv[]) { printf("Start\n"); try { int *pWord=NULL; *pWord=999; } catch(...) { printf("Error was caught\n"); } printf("Stop\n"); return 0; } This should display Start Error was caught Stop But it actually displays Start .. the the MS crash dialog pops up. Does anyone know why my exception traps have been disabled. Richard.


            Hell I thought it was funny .....

            Z Offline
            Z Offline
            zoid
            wrote on last edited by
            #5

            null pointer or access violation exceptions are not treated as C++ exceptions but as SEH exceptions. You cannot catch SEH exceptions using the try catch block, you need to use a __try, __except block instead. There is away of unifying the two different exception handling mechanisms. Search for SEH on codeproject and you should find an article that describes how to do this.

            1 Reply Last reply
            0
            • B Bangerman

              So I made the changes and it made no difference to the problem. However, I managed to connect the debugger and what I've found is frightening. The problem is ocurring because I'm passing an invalid parameter to localtime_s but rather than reporting any error or throwing an exception it is directly calling: _invoke_watson(pszExpression, pszFunction, pszFile, nLine, pReserved); So msvcrt80.dll directly invokes DrWatson rather than throwing an error for you to catch. Now looking at their code I can see that in order to catch invald parameters in my code I'm going to have to add an invalid parameter handler into it. This seems strange to me, surly rather than calling DrWatson, it should throw an exception or even return the error that the docs say it will return for an invalid value.


              Hell I thought it was funny .....

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              I don't generally approve of catching such exceptions (access violations). There are exceptions (not the programming type) – such as in low level code – but in general littering code with “catch alls” just makes debugging hard and postmortem debugging near impossible. A good rule when using exceptions is this: only catch what you expect can be thrown if the program is functioning normally. Breaking this rule can make you wish you never heard of exception handling. For example, if you get an access violation while building a doubly linked list you crash and the debugger or Dr.Watson dump is at the problem point; if the exception is caught the program continues with a corrupted list and if your lucky a crash occurs some time later and you’re left tearing you hair out trying to find the source of the corruption; if you’re unlucky the program doesn’t crash at all but just doesn’t work properly.

              Steve

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups