unexpected exception handler | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| |
Hello everyone,
This question is about when it is allowed to call throw to re-throw exception -- make the exception re-throw.
I can only think of two situations,
1. in catch block of an exception;
2. in unexpected handler.
For (2), in unexpected handler, since the input parameter is null, so in order to get the exception information, we need to re-throw it and catch it to get exception informaiton.
Is my understanding correct? Here is some pseudo code from Bjarne's book. -
// suppose throwY is unexpcted handler
-
void throwY() throw (Yunexpected)
-
{
-
try{
-
throw; // have to re-throw and catch in order to get exception information since current input parameter is null and has no exception information, my understanding correct?
-
} catch (Network_exception& p)
-
{
-
throw (Yunexpected (&p));
-
} catch (...)
-
{
-
throw (Yunexpected (0));
-
}
-
}
-
thanks in advance,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: unexpected exception handler
Your example is not a re-throw. What it shows is catching exception A and throwing exception B.
This is commonly done when the caught exception is not the corrrect one to report. That is, a memory exception may be OK at a low level but at a user level you may need something more general.
This is a re-throw: -
catch (George2 arg)
-
{
-
//write info to disc log
-
throw; //re-throw
-
}
-
You are correct that you can't rethrow unless you are in an exception handler.
However, in the case of unexpected exceptions, these are a thrown as shown in the exception specification of the function doing the throw. It allows you to convert an unexpected exception into an expected one. However, the information on the original exception cannot be retreived. This sort of this is done to rescue your program from the jaws of death (the unexpected handler from which there is no return) by throwing an expected exception.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: unexpected exception handler
Hi weaknessforcats,
My question is to solve the issue you mention below -- unexpected exception is thrown and how to retrieve exception information.
I am not sure whether my code and my understanding is correct, and so I let people here to review and comment. :-)
Do you agee what I mentioned? Quote:
Originally Posted by weaknessforcats However, the information on the original exception cannot be retreived. This sort of this is done to rescue your program from the jaws of death (the unexpected handler from which there is no return) by throwing an expected exception.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: unexpected exception handler Quote:
Originally Posted by Georege2 My question is to solve the issue you mention below -- unexpected exception is thrown and how to retrieve exception information. Maybe I wasn't clear. If you don't know the type of the exception , then there's no recovery of the exception information later. That is, if it's unexpected you just end up in the unexpected() handler whioch ahs no arguments and no return.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: unexpected exception handler
Hi weaknessforcats,
My question is just as you mentioned below, since unexpected() has no input parameter, in order to get exception information -- i.e. which exception brings us into unexpected() -- we have to rethrow it in unexpected handler and catch it in unexpected handler? Is this understanding correct? Quote:
Originally Posted by weaknessforcats Maybe I wasn't clear. If you don't know the type of the exception , then there's no recovery of the exception information later. That is, if it's unexpected you just end up in the unexpected() handler whioch ahs no arguments and no return.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: unexpected exception handler Quote:
Originally Posted by George2 My question is just as you mentioned below, since unexpected() has no input parameter, in order to get exception information -- i.e. which exception brings us into unexpected() -- we have to rethrow it in unexpected handler and catch it in unexpected handler? Is this understanding correct? No you are not correct. Inside the unexpected handler you have no idea what to throw. I mean tou get here because the exception was unexpected. If you rethrow, you'll just end up here again. You have to throw a new exception that will now be expected.
Read up on function exception specifications and before you try using one be sure your compiler supports them. Not all do.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: unexpected exception handler
Hi weaknessforcats,
No I do not agree. If you rethrow current exception in unexpected exception handler, you can catch it in exception handler itself using local catch block in unexpected exception handler.
It is mentioned in Bjarne's book,
section 14.6.3.2 Recovering the Type of an Exception.
Any comments? Quote:
Originally Posted by weaknessforcats No you are not correct. Inside the unexpected handler you have no idea what to throw. I mean tou get here because the exception was unexpected. If you rethrow, you'll just end up here again. You have to throw a new exception that will now be expected.
Read up on function exception specifications and before you try using one be sure your compiler supports them. Not all do.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: unexpected exception handler
Read that example carefully.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: unexpected exception handler
Hi weaknessforcats,
I read it again but I do not know which point you think I am wrong? Could you kindly point out please? :-) Quote:
Originally Posted by weaknessforcats Read that example carefully.
have a good weekend,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: unexpected exception handler
It's becuse the exceptions are mapped.
Read 14.6.3.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: unexpected exception handler
Thanks weaknessforcats,
Yes, exception is mapped. But what is wrong in my points in your senses? Quote:
Originally Posted by weaknessforcats It's becuse the exceptions are mapped.
Read 14.6.3.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: unexpected exception handler
Come on.
This code: -
try
-
{
-
fx();
-
}
-
catch (...)
-
{
-
throw;
-
}
-
rethrows the original exception. BUT inside the catch(...) block you have no idea what type of exception you caught and there is no way to find out. All you can do re-throw and hope another catch block can deal with it or you can throw a new exception that you already know about and go from there.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: unexpected exception handler
Hi weaknessforcats,
In catch (...) block, if you rethrow it and in next handler, you can use what() to get the information.
You can also use several other catch blocks to match the type of possible exceptions, if it is matched, it means it is a specific type of exception -- maybe the type of your customized exception -- sure the exception information could be retrieved. :-)
Any comments? Quote:
Originally Posted by weaknessforcats Come on.
This code: -
try
-
{
-
fx();
-
}
-
catch (...)
-
{
-
throw;
-
}
-
rethrows the original exception. BUT inside the catch(...) block you have no idea what type of exception you caught and there is no way to find out. All you can do re-throw and hope another catch block can deal with it or you can throw a new exception that you already know about and go from there.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: unexpected exception handler Quote:
Originally Posted by George2 In catch (...) block, if you rethrow it and in next handler, you can use what() to get the information.
You can also use several other catch blocks to match the type of possible exceptions, if it is matched, it means it is a specific type of exception -- maybe the type of your customized exception -- sure the exception information could be retrieved. :-) Are you daft??
what() is a method. It doesn't tell you the type of the exception. Also, what() is a method only on the STL exception class and does not exist elsewhere.
Further, no amount of catch blocks is going to guarantee you will catch the unexpected exception. You see, it's unexpected because you are not expecting it and therefore probably have not set up all those catch blocks.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: unexpected exception handler
Thanks weaknessforcats,
I got your idea. Quote:
Originally Posted by weaknessforcats Are you daft??
what() is a method. It doesn't tell you the type of the exception. Also, what() is a method only on the STL exception class and does not exist elsewhere.
Further, no amount of catch blocks is going to guarantee you will catch the unexpected exception. You see, it's unexpected because you are not expecting it and therefore probably have not set up all those catch blocks.
regards,
George
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|