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. catching a divide-by-zero error

catching a divide-by-zero error

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
6 Posts 6 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.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    I am trying to catch a divide-by-zero error with the following code:

    #include
    using namespace std;

    int main() {
    try {
    int a = 0;
    int x = 5 / a;
    }
    catch (exception e)
    {
    cout << e.what() << endl;
    }
    catch (...)
    {
    cout << "caught ..." << endl;
    }

    }

    For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.

    M J Greg UtasG J D 5 Replies Last reply
    0
    • M mike7411

      I am trying to catch a divide-by-zero error with the following code:

      #include
      using namespace std;

      int main() {
      try {
      int a = 0;
      int x = 5 / a;
      }
      catch (exception e)
      {
      cout << e.what() << endl;
      }
      catch (...)
      {
      cout << "caught ..." << endl;
      }

      }

      For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      AFAIK, there's no way to do it. You'll have to catch it manually and throw.

      CI/CD = Continuous Impediment/Continuous Despair

      1 Reply Last reply
      0
      • M mike7411

        I am trying to catch a divide-by-zero error with the following code:

        #include
        using namespace std;

        int main() {
        try {
        int a = 0;
        int x = 5 / a;
        }
        catch (exception e)
        {
        cout << e.what() << endl;
        }
        catch (...)
        {
        cout << "caught ..." << endl;
        }

        }

        For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.

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

        I've not used it, but maybe take a look at Handling the Divide by Zero Exception in C++[^], similar to what Maximilien said.

        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

        1 Reply Last reply
        0
        • M mike7411

          I am trying to catch a divide-by-zero error with the following code:

          #include
          using namespace std;

          int main() {
          try {
          int a = 0;
          int x = 5 / a;
          }
          catch (exception e)
          {
          cout << e.what() << endl;
          }
          catch (...)
          {
          cout << "caught ..." << endl;
          }

          }

          For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.

          Greg UtasG Offline
          Greg UtasG Offline
          Greg Utas
          wrote on last edited by
          #4

          The article mentioned in the previous post does it by doing divisions in a function that checks for a zero divisor and throws an exception if it detects one. If you don't want to use such a function, it gets rather complicated. If you're running on Windows, you need to handle the structured exception STATUS_FLOAT_DIVIDE_BY_ZERO or STATUS_INT_DIVIDE_BY_ZERO. If you're running on Linux, you need to handle the POSIX signal SIGFPE. You can also handle the use of a bad pointer with these techniques. The handler that receives the structured exception or POSIX signal can throw a C++ exception, which you can then catch in the usual way. The following article goes into the details. But because it also describes a thread framework, you'll have to sift through it to extract the specific code that you need. In particular, see the sections "Receiving a Windows Structured Exception" and "Receiving a POSIX Signal". Robust C++: Safety Net[^]

          Robust Services Core | Software Techniques for Lemmings | Articles
          The fox knows many things, but the hedgehog knows one big thing.

          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

          1 Reply Last reply
          0
          • M mike7411

            I am trying to catch a divide-by-zero error with the following code:

            #include
            using namespace std;

            int main() {
            try {
            int a = 0;
            int x = 5 / a;
            }
            catch (exception e)
            {
            cout << e.what() << endl;
            }
            catch (...)
            {
            cout << "caught ..." << endl;
            }

            }

            For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            To me that was always the 'fun' part of C++ versus Java and C# - figuring out exactly what was being thrown in the first place.

            1 Reply Last reply
            0
            • M mike7411

              I am trying to catch a divide-by-zero error with the following code:

              #include
              using namespace std;

              int main() {
              try {
              int a = 0;
              int x = 5 / a;
              }
              catch (exception e)
              {
              cout << e.what() << endl;
              }
              catch (...)
              {
              cout << "caught ..." << endl;
              }

              }

              For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.

              D Offline
              D Offline
              Daniel Pfeffer
              wrote on last edited by
              #6

              In Windows, there are additional, O/S-level exceptions, that provide this sort of information. Look for "Structured Exception Handling". If you use MSVC, you may use the _set_se_handler function in main() and in the main function of each thread to convert Structured Exceptions to C++ exceptions. There may be similar O/S- and compiler-specific methods in other environments.

              Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

              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