473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

C++ exception context

jg
Can someone explain what the standard says about
exception context ? For example, suppose bar() always throw
an exception, is the value of g undefined or 1 after the handler
(empty in this case) returns ?

Thanks
JG

foo()
{
int g=0;
try {
g++;
bar(); // bar() throw an exception
} catch (...)
}
printf("g=%d\n", g);
}

Apr 16 '07 #1
9 1836
jg wrote:
Can someone explain what the standard says about
exception context ? For example, suppose bar() always throw
an exception, is the value of g undefined or 1 after the handler
(empty in this case) returns ?

Thanks
JG

foo()
{
int g=0;
The scope of g is foo(), so g will be 1.
try {
g++;
bar(); // bar() throw an exception
} catch (...)
}
printf("g=%d\n", g);
}

--
Ian Collins.
Apr 16 '07 #2
"jg" <jg****@gmail.comwrote in news:1176764745.712480.47890
@y80g2000hsf.googlegroups.com:
Can someone explain what the standard says about
exception context ? For example, suppose bar() always throw
an exception, is the value of g undefined or 1 after the handler
(empty in this case) returns ?

Thanks
JG

foo()
{
int g=0;
try {
g++;
bar(); // bar() throw an exception
} catch (...)
}
printf("g=%d\n", g);
}

I'm not sure why you would think that bar() would have any effect on g.
The g++ line would have finished executing before bar() is called. An
exception within a try{} block doesn't somehow invalidate the code that was
executed up to the point of the exception.
Apr 16 '07 #3
jg
<<Ian Collins>>
The scope of g is foo(), so g will be 1.
So, for the following, glb will be 2.

int glb=1;

void bar()
{
glb++;
throw 1;
}

foo()
try {
bar();
} catch (...)
}
printf("glb=%d\n", glb);
}
<< Andre Kostur>>
>
I'm not sure why you would think that bar() would have any effect on g.
The g++ line would have finished executing before bar() is called. An
exception within a try{} block doesn't somehow invalidate the code that was
executed up to the point of the exception.
Since a compiler will allocate g into a register, that same register
may be reused (write/use) by bar(). Well, the value of the register
must be saved before being used by bar(). I am just wondering
how a compiler knows where to restore the value of g....

Thanks.
jg
used by bar(), I am wondering how a hand

Apr 17 '07 #4
jg wrote:
<<Ian Collins>>
>>The scope of g is foo(), so g will be 1.


So, for the following, glb will be 2.
Why would it be anything else?
int glb=1;

void bar()
{
glb++;
throw 1;
}

Since a compiler will allocate g into a register, that same register
may be reused (write/use) by bar(). Well, the value of the register
must be saved before being used by bar(). I am just wondering
how a compiler knows where to restore the value of g....
Why would it allocate g into a register? g is a global variable, an
address in memory, that's all.

--
Ian Collins.
Apr 17 '07 #5
On Apr 17, 1:05 am, "jg" <jgu...@gmail.comwrote:
Can someone explain what the standard says about
exception context ? For example, suppose bar() always throw
an exception, is the value of g undefined or 1 after the handler
(empty in this case) returns ?
foo()
{
int g=0;
try {
g++;
bar(); // bar() throw an exception
} catch (...)
}
printf("g=%d\n", g);
}
In this case, no. The usual C++ rules apply: values will
reflect all changes before the last sequence point, and none of
the changes after the next one. Anything which changes with no
intervening sequence point is up in the air. Throwing an
exception is a sequence point, but don't forget that the order
of evaluation of an expression is not specified, so something
like the following can be very problematic:

f( g++, bar() ) ;

If bar() throws, g may or may not have been incremented.

Be careful, too, because sequence points only define a partial
ordering. For example, given:
f( auto_ptr( new Toto ), bar() ) ;
one possible ordering is:
tmp = new Toto
bar()
auto_ptr( tmp )
If bar() throws, you've got a memory leak.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 17 '07 #6

"jg" <jg****@gmail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
foo()
{
int g=0;
try {
g++;
bar(); // bar() throw an exception
} catch (...)
Not an answer to your question, but...
shouldn't there be a { here?
}
printf("g=%d\n", g);
}
-Howard
Apr 17 '07 #7
jg
Thanks for answering my questions.

I also played with it and looked at the code generated. It looks
that the variables, even locals, will be stored into memory before
calling bar(), which is within try block and may throw an exception.

In a C program (not C++), a local variable usually stays within a
register
accross a call (a performance advantage over C++); whereas in C++, a
local
variable cannot stay in a register accross a call within a try block.

jg

On Apr 17, 2:51 am, James Kanze <james.ka...@gmail.comwrote:
On Apr 17, 1:05 am, "jg" <jgu...@gmail.comwrote:
Can someone explain what the standard says about
exception context ? For example, suppose bar() always throw
an exception, is the value of g undefined or 1 after the handler
(empty in this case) returns ?
foo()
{
int g=0;
try {
g++;
bar(); // bar() throw an exception
} catch (...)
}
printf("g=%d\n", g);
}

In this case, no. The usual C++ rules apply: values will
reflect all changes before the last sequence point, and none of
the changes after the next one. Anything which changes with no
intervening sequence point is up in the air. Throwing an
exception is a sequence point, but don't forget that the order
of evaluation of an expression is not specified, so something
like the following can be very problematic:

f( g++, bar() ) ;

If bar() throws, g may or may not have been incremented.

Be careful, too, because sequence points only define a partial
ordering. For example, given:
f( auto_ptr( new Toto ), bar() ) ;
one possible ordering is:
tmp = new Toto
bar()
auto_ptr( tmp )
If bar() throws, you've got a memory leak.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 17 '07 #8
jg <jg****@gmail.comwrote in news:1176834633.458139.176790
@y5g2000hsa.googlegroups.com:
Thanks for answering my questions.

I also played with it and looked at the code generated. It looks
that the variables, even locals, will be stored into memory before
calling bar(), which is within try block and may throw an exception.

In a C program (not C++), a local variable usually stays within a
register
accross a call (a performance advantage over C++); whereas in C++, a
local
variable cannot stay in a register accross a call within a try block.
That's a compiler quality of implementation issue, not a C++ lanugage
issue.
Apr 17 '07 #9
jg wrote:
I also played with it and looked at the code generated. It looks
that the variables, even locals, will be stored into memory before
calling bar(), which is within try block and may throw an exception.
In a C program (not C++), a local variable usually stays
within a register accross a call (a performance advantage over
C++); whereas in C++, a local variable cannot stay in a
register accross a call within a try block.
It depends totally on the implementation, and exceptions have
very little effect here. Different implementations have
different conventions regarding what must be saved across the
context of a function call. Back when I was regularly working
on Intel architecture, for example, the convention was that a
function could use any register it wanted: it only had to
restore BP (the frame pointer), SP (obviously:-)) and the
segment registers. A compiler could not leave a value in a
register, and expect it to be there after a function call.
Today, on my Sparc, 16 registers are guaranteed safe, and a
compiler can leave a value in one of them even when calling a
function which may throw.

Exceptions affect optimization in two ways. First, they add
control flow paths which the compiler must take into account.
There are probably cases where this will result in saving a
value to memory, where it would otherwise be left in a register,
but they probably aren't very frequent. Secondly, it means that
any time a function which may throw is called, the stack
structure must be "clean"; it must be possible to walk back up
the stack using standard mechanisms. In practice, I don't think
that this ever affects optimization; I'm not aware of a compiler
that doesn't systematically build a clean stack structure in a
non-leaf function.

Exceptions can also have a positive effect on optimization.
First, because the test for the error condition simply isn't
present in the code of the intermediate functions, and secondly,
because the data used by the exception handling is out of line,
possibly in a separate segment, so the resulting function is
smaller, and more likely to fit into cache.

In practice, both effects seem to be pretty negligeable, and the
choix between exceptions or another error reporting mechanism
should be based on design, without regarding performance. Do
what's right; the probability that it will cause a performance
problem is so low that it's not worth worrying about.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 18 '07 #10

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

Similar topics

6
by: Páll Ólafsson | last post by:
Hi I have a problem with the Microsoft.ApplicationBlocks.ExceptionManagement? I can't get it to work in RELEASE mode? If I run the project in debug mode the block works fine but when I run the...
2
by: cody | last post by:
Is this good style? I always have to catch the generic System.Exception because our system supports nested transactions that is BeginTransaction increases the level, Commit decreases it, rollback...
0
by: Mike Grishaber | last post by:
I am using .NET Remoting to connect my incoming HTTP Requests to my business logic. I am using a custom IHttpAsyncHandler to receive the incoming HTTPRequests and then I forward the...
44
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...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
1
by: Johan Neidenmark | last post by:
Copied from microsoft.public.cmserver.general...
2
by: Dominik | last post by:
After going productive a few days ago, we had a very strange exception on of our loadbalanced productive servers running a Microsoft Content Management Server ASP.Net application. As this exception...
3
by: Dan | last post by:
Hi all! When I throw my custom Exception class the first time in my code, the compiler takes a lot of time for find the following catch EX: try Throw New MyCustomException("test")
2
by: Richard Collette | last post by:
Hi, I have a service, that runs perfectly when executed outside of the web service environment. When called as a web service I get the exception listed below sporadically. A call to the web...
0
by: =?Utf-8?B?UG9sbHkgQW5uYQ==?= | last post by:
Hi, I have previously used EL v 3.1 Exception Handling application block successfully. I thought I would now try to do the same with EL v 4.0. My first experiment was to replace an exception....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.