| sisodia (6) | |
|
I am trying to use the try catch for any kind of exception by using catch(...) but it is not catching the exception. Is there any method through which i can catch any exception which occurs in a particular function or logic. int main() { try { int a=4,b=0; int c; c=a/b; cout<<"c="<<c; throw; } catch(...) { cout<<"\ndivision by zero"; } _getch(); } | |
|
|
|
| firedraco (5413) | |
AFAIK throw; simply forwards the current exception. Since you have no exception, I don't really know what it would do. Try something like throw std::exception();
| |
|
|
|
| codewalker (159) | |
| If you are thinking that catch(...) will catch division by zero then its not going to. Devision by zero is not a C++ exception | |
|
|
|
| Peter87 (3687) | ||
ยง15.1/9
| ||
|
Last edited on
|
||
| sisodia (6) | |
|
so is there any method through which i can catch any exception without giving the condition as throw std::exception(); is also not working. | |
|
|
|
| Peter87 (3687) | |
| Your catch block will catch all types of exceptions but when you throw an exception you have to specify what it is that you are throwing. | |
|
|
|
| sisodia (6) | |
| means if i remove throw; from it then it should work? | |
|
|
|
| sisodia (6) | |
|
But even then also it is not working, i have tried that too.... int main() { try { int a=4,b=0; int c; c=a/b; cout<<"c="<<c; } catch(...) { cout<<"\ndivision by zero"; } _getch(); } | |
|
|
|
| Peter87 (3687) | |
| As codewalker said, division by zero does not throw an exception. It's undefined behaviour so you should just not do it. | |
|
|
|
| sisodia (6) | |
| means i need to throw an exception if it not in there. Actually i was looking for a way so that i need not to throw any exception but then also the catch block catches it as in java it does. | |
|
|
|