The following code does not catch an exception, when I try to divide by 0. Do I need to throw an exception, or does the computer automatically throw one at runtime?
do i need to throw an exception or does the computer automatically throws one at runtime?
Either you need to throw the exception yourself and catch it. e.g.
try {
//...
throw int();
}
catch(int i) { }
Or catch the exception which is thrown by your code.
try {
int *p = new int();
}
catch (std::bad_alloc e) {
cerr << e.what();
}
In your case, I am not sure if is there any standard exception meant for divide by zero. If there is no such exception then you can use,
catch(...) { // catch 'any' exception
}