1

Why am I not entering in exception in the code below ?

int main()
{

    int n_one, n_two;
    cout << "enter number one \n";
    cin >> n_one;

    cout << "enter a zero for causing exception \n";
    cin >> n_two;


    try {
        int result = n_one / n_two;
    }
    catch (...) {
        cout << "Welcome to the exception !!";
    }

    return 0;
}

I want to use the try/catch handling any kind of exception with catch(...) for another project, how can I make this work ?

TaZz
  • 652
  • 7
  • 20
  • Try volatile int result. Compiler might optimize useless code away otherwise. – Unimportant Apr 04 '16 at 10:56
  • 1
    Dividing by zero causes undefined behaviour. It is not required to result in a C++ exception being thrown. In practice, the behaviour depends on interaction between the program, the host operating system, and the host hardware. If you want an exception to be thrown, do something like `if (n_two == 0) throw your_chosen_exception();` BEFORE attempting the division. – Peter Apr 04 '16 at 11:01

0 Answers0