473,725 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++: rethrow in exception handling

A
Hi,

I'm having some difficulty understanding the semantics of the rethrow
keyword. Consider the following code:

int main(){
try{
...
}
catch(SomeExcep tion &SE){
...
}
catch(...){
rethrow;
}
return 0;
}
What is the point of rethrowing the exception caught at catch(...)? Won't
this result in a endless cycle effect?
Regards,
A
Jul 22 '05 #1
5 11020
A wrote:
Hi,
hi
What is the point of rethrowing the exception caught at catch(...)? Won't
this result in a endless cycle effect?

no.

you can use it to do some cleaning before letting the exception reach
the next handler, as if you didn't caught it. in your example you'll let
the exception be trapped by the default handler which usually aborts you
app.
-----[ Domenico Andreoli, aka cavok
--[ http://filibusta.crema.unimi.it/~cavok/gpgkey.asc
---[ 3A0F 2F80 F79C 678A 8936 4FEE 0677 9033 A20E BC50

Jul 22 '05 #2
On Tue, 18 Nov 2003 21:35:03 +1030, "A" <A@iprimus.com. au> wrote:
Hi,

I'm having some difficulty understanding the semantics of the rethrow
keyword.
There is no rethrow keyword. I'm going to assume you mean throw.
Consider the following code:
int main(){
try{
...
}
catch(SomeExcep tion &SE){
...
}
catch(...){
rethrow;
Rather:
throw;
}
return 0;
}
What is the point of rethrowing the exception caught at catch(...)? Won't
this result in a endless cycle effect?


No, the throw above will just cause the program to terminate (since
the exception will propogate out of main. throw; is used when you want
to do a bit of clean up in the current scope, and then let the
exception propogate up. However, it should be used sparingly - RAII is
a much better technique to handle exception cleanup.

Tom
Jul 22 '05 #3
A

"tom_usenet " <to********@hot mail.com> wrote in message
news:r4******** *************** *********@4ax.c om...
On Tue, 18 Nov 2003 21:35:03 +1030, "A" <A@iprimus.com. au> wrote:
Hi,

I'm having some difficulty understanding the semantics of the rethrow
keyword.


There is no rethrow keyword. I'm going to assume you mean throw.
Consider the following code:

int main(){
try{
...
}
catch(SomeExcep tion &SE){
...
}
catch(...){
rethrow;


Rather:
throw;
}
return 0;
}
What is the point of rethrowing the exception caught at catch(...)? Won't
this result in a endless cycle effect?


No, the throw above will just cause the program to terminate (since
the exception will propogate out of main. throw; is used when you want
to do a bit of clean up in the current scope, and then let the
exception propogate up. However, it should be used sparingly - RAII is
a much better technique to handle exception cleanup.

Tom


What do you mean by propogate up? what goes up must come down again? and so
it repeats itself.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.538 / Virus Database: 333 - Release Date: 10/11/2003
Jul 22 '05 #4
hi!
catch(...){
throw; changed this to throw... }
return 0;
}
What do you mean by propogate up? what goes up must come down again? and

so it repeats itself.

here you throw the expection within the catch. now looking for an
appropriate exception handler starts all over again.
it's not been called within a try in main, so the thrown exception
propagates out of main, and there the default exception handler "is
waiting", which normally terminates the app.

note: throwing an exception in a catch() block does not result in getting
caught within this catch() block, it rather "needs to be thrown" within a
try block for the exception handling to work (except for the default handler
outside of main)

regards,
sev
Jul 22 '05 #5
On Wed, 19 Nov 2003 19:27:41 +1030, "A" <A@iprimus.com. au> wrote:
What do you mean by propogate up? what goes up must come down again? and so
it repeats itself.


I mean propogate up the stack to the function that called the one the
throw; is in. I think you misunderstand what throw; actually does. It
doesn't rethrow the exception from where it was originally thrown, it
throws it from the point of the throw;. Here's an example:

#include <iostream>

class test_exception{ };

void foo(int size)
{
std::cout << '3';
int* i = new int[size];
std::cout << '4';
try
{
std::cout << '5';
throw test_exception( ); //*
std::cout << "Never here";
}
catch(...)
{
std::cout << '6';
delete[] i;
std::cout << '7';
throw; //throws the exception from here, not from *
std::cout << "Never here";
}
std::cout << "Never here";
}

int main()
{
std::cout << '1';
try
{
std::cout << '2';
foo(100);
std::cout << "Never here";
}
catch(test_exce ption const&)
{
std::cout << '8';
}
std::cout << '\n';
}

Tom
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
48457
by: Gil | last post by:
In C++, I can rethrow the exception I just caught with the throw statement. Can I do something similar in Java? } catch (Exception ex) { throw; }
3
2749
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech exception handling mechanism which will allow me to pass character information about an error and its location from lower-level classes. Can you please critique the following exception handling mechanism in terms of my requirements ?
8
5835
by: Taylor | last post by:
I've run in to code with this pattern: try { // do some potentially bad stuff } catch(System.Exception ex) { throw ex; }
44
4216
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
10
6921
by: ahaupt | last post by:
Hi all, Why would one want to rethrow an exception? Doesn't that defeat the purpose? Best, Andre
6
1628
by: cmay | last post by:
I'm just looking for some other opinions on this. I have a control that does employee lookups, auto complete of the name etc. Let say that this control is: Company.Controls.Web.LookupControls.EmployeeLookupControl Now I have a Employee class: Company.Business.Employee
24
2373
by: Chameleon | last post by:
Is there a possibility to create memory leak, the code below if I run the line: --------------------------------------------------------- MyClass cl = new MyClass(); --------------------------------------------------------- ??? --------------------------------------------------------- MyClass::MyClass() {
1
4580
by: Jason S | last post by:
I haven't used try/catch/finally very much in Javascript. My function (let's call it try_it()) needs to call a function that could throw an exception (let's call it dangerous()) with some setup() beforehand and cleanup() afterwards. What I want to make sure cleanup() is called whether or not dangerous throws an exception, and if it does throw an exception, rethrow the exception to whatever is calling try_it(). In C++ this is much easier...
8
3399
by: reuce-google | last post by:
Hi folks, I want to store an exception and rethrow it later: CException m_pEc = NULL; // Class variable. try { throw new CMemoryException(); }
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.