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. don't catch in release?

don't catch in release?

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingquestionannouncement
10 Posts 5 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.
  • N Offline
    N Offline
    novachen
    wrote on last edited by
    #1

    Hi! I just try to add some exception procession in my console program. But it seems work in debug but not in release. I don't know what's up. In debug, it output a "catch it" and stop itself. but in release, there is a dialog popup out "Integer Divide by Zero". int a = 0; int b; try { b = 10 / a; printf("b = %d a = %d\n", b, a); } catch (...) { printf("catch it"); return -1; } return 0;

    T J T 3 Replies Last reply
    0
    • N novachen

      Hi! I just try to add some exception procession in my console program. But it seems work in debug but not in release. I don't know what's up. In debug, it output a "catch it" and stop itself. but in release, there is a dialog popup out "Integer Divide by Zero". int a = 0; int b; try { b = 10 / a; printf("b = %d a = %d\n", b, a); } catch (...) { printf("catch it"); return -1; } return 0;

      T Offline
      T Offline
      Toni78
      wrote on last edited by
      #2

      To catch an error you have to throw an exeption by calling throw. In debug mode certain exeptions are handled by the debuger, but that's not the case in your release version. If an exception is not caught by any catch statement because there is no catch statement with a matching type, the special function terminate will be called. This function is generally defined so that it terminates the current process immediately showing an "Abnormal termination" error message. Its format is: void terminate();

      int a = 0;
      int b;
      try
      {
      b = 10 / a;
      if( a == 0 ) throw "Error"; // You need this
      printf("b = %d a = %d\n", b, a);
      }
      catch (...)
      {
      printf("catch it");
      return -1;
      }

      // Afterall I realized that even my comment lines have bugs

      J 1 Reply Last reply
      0
      • N novachen

        Hi! I just try to add some exception procession in my console program. But it seems work in debug but not in release. I don't know what's up. In debug, it output a "catch it" and stop itself. but in release, there is a dialog popup out "Integer Divide by Zero". int a = 0; int b; try { b = 10 / a; printf("b = %d a = %d\n", b, a); } catch (...) { printf("catch it"); return -1; } return 0;

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #3

        For me, it works in Debug as well as Release build. The message is "First-chance exception in test4.exe: 0xC0000094: Integer Divide by Zero." Did you disable exceptions in the Project settings under the 'C++ Language'-tab?


        My opinions may have changed, but not the fact that I am right.

        N 1 Reply Last reply
        0
        • T Toni78

          To catch an error you have to throw an exeption by calling throw. In debug mode certain exeptions are handled by the debuger, but that's not the case in your release version. If an exception is not caught by any catch statement because there is no catch statement with a matching type, the special function terminate will be called. This function is generally defined so that it terminates the current process immediately showing an "Abnormal termination" error message. Its format is: void terminate();

          int a = 0;
          int b;
          try
          {
          b = 10 / a;
          if( a == 0 ) throw "Error"; // You need this
          printf("b = %d a = %d\n", b, a);
          }
          catch (...)
          {
          printf("catch it");
          return -1;
          }

          // Afterall I realized that even my comment lines have bugs

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          Toni78 wrote: ...because there is no catch statement with a matching type... But there is: He has written catch(...) to catch ALL exceptions!


          My opinions may have changed, but not the fact that I am right.

          T 1 Reply Last reply
          0
          • N novachen

            Hi! I just try to add some exception procession in my console program. But it seems work in debug but not in release. I don't know what's up. In debug, it output a "catch it" and stop itself. but in release, there is a dialog popup out "Integer Divide by Zero". int a = 0; int b; try { b = 10 / a; printf("b = %d a = %d\n", b, a); } catch (...) { printf("catch it"); return -1; } return 0;

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            VC6? There is a bug in the optimizer with VC6 that causes simple try/catch blocks from working. What happens is that the compiler thinks there can't be an exception so it optimizes the try/catch block away. Tim Smith I'm going to patent thought. I have yet to see any prior art.

            S 1 Reply Last reply
            0
            • T Tim Smith

              VC6? There is a bug in the optimizer with VC6 that causes simple try/catch blocks from working. What happens is that the compiler thinks there can't be an exception so it optimizes the try/catch block away. Tim Smith I'm going to patent thought. I have yet to see any prior art.

              S Offline
              S Offline
              saierdia
              wrote on last edited by
              #6

              i have test the code under vc7,vc7.1 debug version do it well, but it seemed that release version has no exception handler code. if use cout replace printf ,then work fine . i dont know whether this is a compiler bug .

              1 Reply Last reply
              0
              • J jhwurmbach

                Toni78 wrote: ...because there is no catch statement with a matching type... But there is: He has written catch(...) to catch ALL exceptions!


                My opinions may have changed, but not the fact that I am right.

                T Offline
                T Offline
                Toni78
                wrote on last edited by
                #7

                jhwurmbach wrote: But there is: He has written catch(...) to catch ALL exceptions! These are the definitions for try, catch, and throw: "The code within the try block is executed normally. In case that an exception takes place, this code must use throw keyword and a parameter to throw an exception. The type of the parameter details the exception and can be of any valid type. We can also define a catch block that captures all the exceptions independently of the type used in the call to throw. For that we have to write three points instead of the parameter type and name accepted by catch." You have to throw an exception because you can't just simply expect the compiler to throw every kind of exception that there is out there. Of course you can argue and say that division by zero is a standard exception and I agree with you on that, but catch(...) doesn't catch ALL the ecxeptions unless you throw some of them. Otherwise, programs would never crash. // Afterall I realized that even my comment lines have bugs

                N 1 Reply Last reply
                0
                • T Toni78

                  jhwurmbach wrote: But there is: He has written catch(...) to catch ALL exceptions! These are the definitions for try, catch, and throw: "The code within the try block is executed normally. In case that an exception takes place, this code must use throw keyword and a parameter to throw an exception. The type of the parameter details the exception and can be of any valid type. We can also define a catch block that captures all the exceptions independently of the type used in the call to throw. For that we have to write three points instead of the parameter type and name accepted by catch." You have to throw an exception because you can't just simply expect the compiler to throw every kind of exception that there is out there. Of course you can argue and say that division by zero is a standard exception and I agree with you on that, but catch(...) doesn't catch ALL the ecxeptions unless you throw some of them. Otherwise, programs would never crash. // Afterall I realized that even my comment lines have bugs

                  N Offline
                  N Offline
                  novachen
                  wrote on last edited by
                  #8

                  ;)Pal, sorry. I still not very clear about it. Does it mean we can't catch a "divide by zero" exception at all? We have to exam every number whether it is zero before we do a divide if we want the program wont crash with a 0? Any code to solve my example?

                  T 1 Reply Last reply
                  0
                  • J jhwurmbach

                    For me, it works in Debug as well as Release build. The message is "First-chance exception in test4.exe: 0xC0000094: Integer Divide by Zero." Did you disable exceptions in the Project settings under the 'C++ Language'-tab?


                    My opinions may have changed, but not the fact that I am right.

                    N Offline
                    N Offline
                    novachen
                    wrote on last edited by
                    #9

                    yes, Exception is enable in 'C++ Language' as default. I won't like a "First-chance exception in test4.exe: 0xC0000094: Integer Divide by Zero." message. That's just i get in release version. What i want is a console output "catch it" and end the program like the behavior in debug version.

                    1 Reply Last reply
                    0
                    • N novachen

                      ;)Pal, sorry. I still not very clear about it. Does it mean we can't catch a "divide by zero" exception at all? We have to exam every number whether it is zero before we do a divide if we want the program wont crash with a 0? Any code to solve my example?

                      T Offline
                      T Offline
                      Toni78
                      wrote on last edited by
                      #10

                      novachen wrote: Does it mean we can't catch a "divide by zero" exception at all? I am not sure how the VC6 compiler handles this particular case so take a look at CException. If you are not using MFC then you have to write your own code. novachen wrote: We have to exam every number whether it is zero before we do a divide if we want the program wont crash with a 0? If you want to handle a divide by zero exception in your own way (not the compilers way) yes you have to check for every number before you divide, and you have to throw the error. novachen wrote: Any code to solve my example? You could check my first reply to your message. // Afterall I realized that even my comment lines have bugs

                      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