468,491 Members | 2,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

unexpected exception handler

200 100+
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.

Expand|Select|Wrap|Line Numbers
  1. // suppose throwY is unexpcted handler
  2. void throwY() throw (Yunexpected)
  3. {
  4.     try{
  5.         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?
  6.     } catch (Network_exception& p)
  7.     {
  8.         throw (Yunexpected (&p));
  9.     } catch (...)
  10.     {
  11.         throw (Yunexpected (0));
  12.     }
  13. }
  14.  

thanks in advance,
George
Jan 8 '08 #1
14 3165
weaknessforcats
9,207 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. catch (George2 arg)
  2. {
  3.      //write info to disc log
  4.      throw;                          //re-throw
  5. }
  6.  
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.
Jan 8 '08 #2
George2
200 100+
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?

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
Jan 9 '08 #3
weaknessforcats
9,207 Expert Mod 8TB
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.
Jan 9 '08 #4
George2
200 100+
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?

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
Jan 10 '08 #5
weaknessforcats
9,207 Expert Mod 8TB
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.
Jan 10 '08 #6
George2
200 100+
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?

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
Jan 11 '08 #7
weaknessforcats
9,207 Expert Mod 8TB
Read that example carefully.
Jan 11 '08 #8
George2
200 100+
Hi weaknessforcats,


I read it again but I do not know which point you think I am wrong? Could you kindly point out please? :-)

Read that example carefully.

have a good weekend,
George
Jan 13 '08 #9
weaknessforcats
9,207 Expert Mod 8TB
It's becuse the exceptions are mapped.

Read 14.6.3.
Jan 13 '08 #10
George2
200 100+
Thanks weaknessforcats,


Yes, exception is mapped. But what is wrong in my points in your senses?

It's becuse the exceptions are mapped.

Read 14.6.3.

regards,
George
Jan 14 '08 #11
weaknessforcats
9,207 Expert Mod 8TB
Come on.

This code:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.         fx();
  4. }
  5. catch (...)
  6. {
  7.      throw;
  8. }
  9.  
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.
Jan 14 '08 #12
George2
200 100+
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?

Come on.

This code:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.         fx();
  4. }
  5. catch (...)
  6. {
  7.      throw;
  8. }
  9.  
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
Jan 17 '08 #13
weaknessforcats
9,207 Expert Mod 8TB
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.
Jan 17 '08 #14
George2
200 100+
Thanks weaknessforcats,


I got your idea.

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
Jan 18 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Attila Feher | last post: by
3 posts views Thread by Teddy | last post: by
2 posts views Thread by Stephen Miller | last post: by
32 posts views Thread by Rene Pijlman | last post: by
2 posts views Thread by =?Utf-8?B?QXJtaW4gR2FsbGlrZXI=?= | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.