1

Why does the catch-all not handle this exception when input is zero?

try
{
    int input;
    std::cin >> input;
    // input is zero
    int x = 50 / input;
    cout << "you will never see this if input is zero";

}
catch (...)
{
    cout << "or this for some reason";
}

EDIT: I'm not trying to handle a divide by zero scenario, just wondering why catch all didn't work for that particular case. Thanks dude.

user1919249
  • 327
  • 1
  • 3
  • 10
  • 1
    There is no exception in your code. (Your code has a bug.) – Kerrek SB Mar 07 '17 at 08:22
  • 2
    Because it's a CPU exception, which is different from a C++ exception and can't be caught with `catch`. You need a better way to validate input (especially considering that C++ exceptions are expensive if thrown, and should only be used for truly *exceptional* events). – Some programmer dude Mar 07 '17 at 08:24
  • Note in the duplicate there is an answer showing how to convert a CPU error to a C++ exception using lambdas and a shared_ptr. The answer shows both gcc and Windows solutions. – Erik Alapää Mar 07 '17 at 10:10

0 Answers0