mercredi 5 août 2020

i want to throw the exception automatically [duplicate]

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()

Aucun commentaire:

Enregistrer un commentaire