I have defined a simple int error code:
#define my_error 20
Somewhere in my code I throw this error:
throw my_error;
Now I want to try and catch this exception thrown:
try
{
...
...
}
catch (my_error)
{
cout << "Error: my error";
}
Unfortunately the compiler doesn’t approve this:
- syntax error : ‘constant’
- catch handlers must specify one type
- ‘try’ block starting on line ’34’ has no catch handlers
Is there a way to do this?
20is not a type, so you can’t catch it. What you can do is filter the values in thecatchblock:An even better approach would be to define your own exception: