exception handling

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();
}
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();
If you are thinking that catch(...) will catch division by zero then its not going to. Devision by zero is not a C++ exception
ยง15.1/9
If no exception is presently being handled, executing a throw-expression with no operand calls std::terminate().
Last edited on
so is there any method through which i can catch any exception without giving the condition as throw std::exception(); is also not working.
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.
means if i remove throw; from it then it should work?
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();
}
As codewalker said, division by zero does not throw an exception. It's undefined behaviour so you should just not do it.
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.
Topic archived. No new replies allowed.