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
14 3567
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.
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
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.
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
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.
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
Read that example carefully.
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
It's becuse the exceptions are mapped.
Read 14.6.3.
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
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.
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: -
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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 {
};
|
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...
|
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...
|
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...
|
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...
|
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...
|
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=()=>{
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |