Learning “try & catch”. What is wrong with the following code?
Thanks for the advice.
Error in execution:
terminate called without an active exception
Aborted
The code:
#include <stdio.h>
int main()
{
int a = 3;
try
{
if (a < 5)
throw;
}
catch (...)
{
printf ("captured\n");
}
return 0;
}
Inside of a
tryblock, you have to specify what to throw. The only place you can usethrowby itself is inside of acatchblock to re-throw the current exception. If you callthrowby itself without a current exception being active, you will kill your app, as you have already discovered.Try this:
You can
throwanything you want, as long as you throw something.