0

this is an exception case where the divisor is 0 while doing a division.

in the below code, what should i do so that it will throw and catch the exception automatically without if statement inside the try block.

#include <iostream>
#include <exception>
using namespace std;

int main(int argc, char const *argv[])
{
int a=3;
int b=0;

int quotient;
try{

    quotient = a/b;
    

}
catch(exception &e){
    cout<<e.what();
}
return 0;
}

its working if i use "if statement" inside the try block. but i want it to be detected automatically and should display the type of exception using e.what()

  • Dividing by zero does not automatically throw any exceptions in C++. You have to handle that case manually. – 0x5453 Aug 05 '20 at 12:54
  • *this is an exception case where the divisor is 0 while doing a division* -- Division by zero is not an exception case. – PaulMcKenzie Aug 05 '20 at 12:56
  • 1
    For the record: in C++ integer division by 0 is Undefined Behaviour. MSVC compiler just happens to deal with that using Windows-specific SEH exceptions. – Yksisarvinen Aug 05 '20 at 13:00

0 Answers0