0

I am learning about try-catch constructs in C++ and I have the following example that appears to fail to execute the code inside either of the catches. I have spent the past few hours trying to find the bug/issue without luck.

I am wondering if there is an issue with g++ on my machine -- I am using mingw's g++ and Windows 10.

#include <iostream>
#include <stdexcept>

int main(){

    try {
        std::cout << "Start of Try-Catch\n";
        int a = 13;
        int b = 0;
        int p = a/b;
        std::cout << "printing p: " << p << std::endl;
        p = 43;

        std::cout << "Passed the div by zero issue\n";
    } catch (std::runtime_error& e){
        std::cout << "runtime error: " << e.what() << '\n';
        return 2;
    } catch (std::exception& e){
        std::cout << "other error: " << e.what() << '\n'; 
        return 3;
    } catch (...) {
        std::cout << "final catch\n";
        return 4;
    }
    std::cout << "end of program\n";
    return 0;
}

Instead, this is what happens when I compile and run:

C:\Users\...\Part 1>g++ cp_bug.cpp -std=c++17

C:\Users\...\Part 1>a.exe
Start of Try-Catch

C:\Users\...\Part 1>
Flatley
  • 3
  • 3
  • 3
    you're throwing nothing to catch it – ic_Engineer May 02 '21 at 15:29
  • 2
    A division by zero doesn't throw an exception. It exhibits undefined behavior - which means [anything is allowed to happen](http://catb.org/jargon/html/N/nasal-demons.html), including getting or not getting into a catch block. – Igor Tandetnik May 02 '21 at 15:31
  • Let me get this straight -- the only way an exception occurs is if I use the throw keyword? TY – Flatley May 02 '21 at 15:36
  • @Flatley c++ would not check every potential error and throw them as exceptions. As Igor pointed out, many of those are just [undefined behaviors](https://en.wikipedia.org/wiki/Undefined_behavior). The primary reason behind this is that error checking incurs cost, and one of the philosophies of c++ is not to incur cost that the programmer does not ask for (and on the same note, different platforms may treat those errors differently, but c++ needs to work cross-platform). – ph3rin May 02 '21 at 15:40
  • Many coding errors result in a runtime message about an "exception". But that exception is not a C++ exception. If your code doesn't explicitly `throw` an exception, either directly or in a library function, there's nothing to catch. – Pete Becker May 02 '21 at 16:47

2 Answers2

2

it would be more logical to do something like that:

int main(){

    try {
        std::cout << "Start of Try-Catch\n";
        int a = 13;
        int b = 0;
        if(b==0)
            throw std::string("Passed the div by zero issue\n");
        int p = a/b;
        
        std::cout << "printing p: " << p << std::endl;
    } catch (std::string e) {
        std::cout << e;
        return -1;
    }
    std::cout << "end of program\n";
    return 0;
}
ic_Engineer
  • 313
  • 3
  • 12
  • In general, exceptions should be caught by `const` reference, not by value. So `catch(const std::string& e)` rather than `catch(std::string e)`. Don't worry: the exception object will hang around until the end of the `catch` clause. _1. – Pete Becker May 02 '21 at 16:49
  • Oh, while I'm nitpicking, `return -1;` is not portable. There are three values you can return from `main`: `EXIT_FAILURE`, `EXIT_SUCCESS`, and 0. `return 0;`, like `EXIT_SUCCESS`, indicates success. – Pete Becker May 02 '21 at 16:50
1

Your problem is that division by zero doesn't throw an exception that can be handled. Try the following tutorial instead.

Also this question is duplicated.

jorgmo02
  • 41
  • 7
  • Thanks -- I did not specify this in the question, but I had tried to get other errors to trigger one of the catch-blocks (i.e. assigning value to null pointer), so to me the other question was not 1:1. I guess I need to use the throw word to actually trigger one of the catch blocks. I appreciate the feedback! – Flatley May 02 '21 at 15:45
  • 1
    Note that the "tutorial" you linked to deals with IEEE 754 floating point division by zero, whereas this question deals with integer division by zero. Therefore, some of the information from that link does not apply here, for example the part about "infinity". – Andreas Wenzel May 02 '21 at 16:00