Reputation: 5918
I am calling a function and I am throwing an exception in that function. But I don't want to catch that in the same function but want to catch it where that function was called, like here is my example code.
void foo()throw(...){
std::cout << "FOO" <<std::endl;
throw "Found";
}
void main(){
try{
foo();
}
catch(...){
std::cout << "exception catched" <<std::endl;
}
}
But it is crashing at the point where I am throwing the exception in foo function, but I want to catch it in the main function.
How would I do that?
Upvotes: 1
Views: 194
Reputation: 5918
Got answer my own question at http://msdn.microsoft.com/en-US/library/wfa0edys%28v=VS.80%29.aspx
Upvotes: 0
Reputation: 355009
throw;
throw
with no operand rethrows the exception that is currently being handled. That means it can only be used in a catch
block. Since you aren't in a catch
block when the throw;
is executed, the program is terminated.
You need to throw something, like a runtime error: throw std::runtime_error("oops");
.
Note also that exception specifications (e.g. the throw(...)
in void foo() throw(...)
) should not be used. For an explanation as to why, see "A Pragmatic Look at Exception Specifications."
Upvotes: 2