472,958 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 3567
weaknessforcats
9,208 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,208 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,208 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,208 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,208 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,208 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,208 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

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

Similar topics

0
by: Robert Mazur | last post by:
MySQL 5.0 alpha (binary install) on Solaris 9 -or- RedHat 8.0 mysql-connector-java-3.0.8-stable ----------------------- Is there something different going on with JDBC and the alpha version...
2
by: Ascaron | last post by:
Hi there! I’m having a strange problem with a c++ dll that is called from a c# program. The dll wraps a large piece of c++ software that uses exceptions for its error-signalling. To keep the...
2
by: Attila Feher | last post by:
Hi all, I have not done much work around exceptions; and even when I do I avoid exception specifications. But now I have to teach people about these language facilities, so I am trying them out...
3
by: Teddy | last post by:
Hello all According to "Think in C++ Volume2", the code below should run smoothly: #include <iostream> #include <exception> using namespace std; class ex { };
2
by: Stephen Miller | last post by:
Can the CustomValidator be used to simply report unexpected errors, without requiring Client/Server validation? To explain, say you had a simple text box and button that did a Full-text Search of a...
2
by: paul.mason | last post by:
I was wondering if anyone else had come across this "feature" of .NET and if they had any idea what might be causing it. I've been writing my first C# Windows application (so if there's anything...
32
by: Rene Pijlman | last post by:
One of the things I dislike about Java is the need to declare exceptions as part of an interface or class definition. But perhaps Java got this right... I've writen an application that uses...
2
by: bb | last post by:
Hi, I am using gcc v4.0.2 on fedora core 4 (2.6.16). Any reason why the handler set thru' set_unexpected() never gets called in the following code? --------- Code ------------- #include...
2
by: =?Utf-8?B?QXJtaW4gR2FsbGlrZXI=?= | last post by:
Hi I've got an unexpected error in a unit test. I want to test a activity from Windows Workflow Foundation (WF). First, I executed the test outside of the activity just in the test-init...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.