Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5940481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:58:26+00:00 2026-05-22T15:58:26+00:00

The following code does not catch an exception, when I try to divide by

  • 0

The following code does not catch an exception, when I try to divide by 0. Do I need to throw an exception, or does the computer automatically throw one at runtime?

int i = 0;

cin >> i;  // what if someone enters zero?

try {
    i = 5/i;
}
catch (std::logic_error e) {

    cerr << e.what();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:58:27+00:00Added an answer on May 22, 2026 at 3:58 pm

    You will need to check it yourself and throw an exception. Integer divide by zero is not an exception in standard C++. Neither is floating point divide/remainder by zero but at least that has specific rational values that may result (such as the various NaN/Inf values).

    The exceptions listed in the [stdexcept.syn] section of ISO C++20 standard (the iteration used in this answer) are:

    namespace std {
        class logic_error;
            class domain_error;
            class invalid_argument;
            class length_error;
            class out_of_range;
        class runtime_error;
            class range_error;
            class overflow_error;
            class underflow_error;
    }
    

    Now you could argue quite cogently that either overflow_error (the infinity generated by IEEE754 floating point could be considered overflow) or domain_error (it is, after all, a problem with the input value) would be ideal for indicating a divide by zero.

    However, section [expr.mul] specifically states (for both integer and floating point division, and integer remainder):

    If the second operand of / or % is zero, the behavior is undefined.

    So, it could throw those (or any other) exceptions. It could also format your hard disk and laugh derisively 🙂


    If you wanted to implement such a beast, you could use something like intDivEx in the following program (using the overflow variant):

    #include <iostream>
    #include <stdexcept>
    
    // Integer division/remainder, catching divide by zero.
    
    inline int intDivEx (int numerator, int denominator) {
        if (denominator == 0)
            throw std::overflow_error("Divide by zero exception");
        return numerator / denominator;
    }
    
    inline int intModEx (int numerator, int denominator) {
        if (denominator == 0)
            throw std::overflow_error("Divide by zero exception");
        return numerator % denominator;
    }
    
    int main (void) {
        int i = 42;
    
        try {
            i = intDivEx (10, 0);
        } catch (std::overflow_error &e) {
            std::cout << e.what() << " -> ";
        }
        std::cout << i << std::endl;
    
        try {
            i = intDivEx (10, 2);
        } catch (std::overflow_error &e) {
            std::cout << e.what() << " -> ";
        }
        std::cout << i << std::endl;
    
        return 0;
    }
    

    This outputs:

    Divide by zero exception -> 42
    5
    

    You can see it throws and catches the exception (leaving the return variable untouched) for the divide by zero case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code which does not rollback the transaction if an exception is
The following code does not compile: public class GenericsTest { public static void main(String[]
The following Code does not compile Dim BasicGroups As String() = New String() {Node1,
How can you enumerate an enum in C#? E.g. the following code does not
I have the following JavaScript code: Link In which the function makewindows does not
I have the following bit of legacy C++ code that does not compile: #include
Why does the following code not work as I was expecting? <?php $data =
Why does the following code NOT give an error, nor any type of a
Why does the following code sometimes causes an Exception with the contents CLIPBRD_E_CANT_OPEN: Clipboard.SetText(str);
I have the following code: public static void main(String[] args) { try { int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.