473,221 Members | 1,736 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,221 software developers and data experts.

Explain internals of throw/catch implementation

Hello

I want to understand the exception habdling in C++ more, because i
think no one can apply anything free (free - without remembering large
unclear list of rules and exceptions from rules and exceptions from
exception from rules) if he does not understand "why" the thing have
done excactly as is.

I know, some people do not agree with me at the point of "why".

1.
I think, the exception is statical linknig, in other words, each throw
"knows" it appropriate catch during comile time.

Is it true?

I think it is true, due to exit() called, if no external try{} block
exist (no "stack unwinding" has occured).

What exactly happends while exceptions? In asm list i can see something
like this:

call function

normal return point:
clear stack if needed
uncondition jmp to exit

return from exception point:
clear stack if needed
push retruned value
call ___cxa_begin_catch

exit:

2.
The question is next:

I have destructor, which can be executed after exception have thrown,
during "stack unwinding". My destructor can create temporary objects,
and each of the created object can throw, in general.

If i throw while i have std::uncaught_exception()!=0, the exit() (oh,
mamma mia :) will be called.

But if i declare new try{} block inside destructor, can temporary
objects safe throw or can not and why?

~Base()
{
try{
A a;
B b;
Base::free(a,b);
}catch(...){ if( !std::uncaught_exception() ) throw; }
}

Why?

Thanks.

Jan 14 '07 #1
8 1959
Tell me anything

Jan 16 '07 #2
On Jan 14, 3:12 pm, "Grizlyk" <grizl...@yandex.ruwrote:
Hello

I want to understand the exception habdling in C++ more, because i
think no one can apply anything free (free - without remembering large
unclear list of rules and exceptions from rules and exceptions from
exception from rules) if he does not understand "why" the thing have
done excactly as is.

I know, some people do not agree with me at the point of "why".

1.
I think, the exception is statical linknig, in other words, each throw
"knows" it appropriate catch during comile time.

Is it true?
Sounds logical to me, but remember that you should never concern
yourself with how it's done on _your_ machine, but rather how it's done
according to the standard. Different vendors might do it in different
ways, and different versions of the same compiler (or just changing
some settings) can do it in different ways. So what you should
concentrate on is what it means from the point of view of your C++
application, everything else can change.

--
Erik Wikström

Jan 16 '07 #3

Grizlyk skrev:
Hello

I want to understand the exception habdling in C++ more, because i
think no one can apply anything free (free - without remembering large
unclear list of rules and exceptions from rules and exceptions from
exception from rules) if he does not understand "why" the thing have
done excactly as is.
Stan Lippman wrote a book about "C++ internals" - that is how you might
implement stuff such as exceptions, virtual functions, constructors and
so on. It is old, and I haven't read it myself but I did have a few
hours of skimming, and what I saw looked real nice. You have to google
for the title, however.
You could also read the standard committees report on C++ performance.
It has a very good chapter on exceptions and is available online. Again
you'll have to google.
>
I know, some people do not agree with me at the point of "why".

1.
I think, the exception is statical linknig, in other words, each throw
"knows" it appropriate catch during comile time.

Is it true?
No.
>
I think it is true, due to exit() called, if no external try{} block
exist (no "stack unwinding" has occured).

What exactly happends while exceptions? In asm list i can see something
like this:

call function

normal return point:
clear stack if needed
uncondition jmp to exit

return from exception point:
clear stack if needed
push retruned value
call ___cxa_begin_catch

exit:

2.
The question is next:

I have destructor, which can be executed after exception have thrown,
during "stack unwinding". My destructor can create temporary objects,
and each of the created object can throw, in general.

If i throw while i have std::uncaught_exception()!=0, the exit() (oh,
mamma mia :) will be called.

But if i declare new try{} block inside destructor, can temporary
objects safe throw or can not and why?
A temporary object can throw, of course.
>
~Base()
{
try{
A a;
B b;
Base::free(a,b);
}catch(...){ if( !std::uncaught_exception() ) throw; }
}
This looks fine, but beware that not all compilers support
std::uncaught_exception(). Also, it is problematic that destructors
should need to throw. In my opinion, they should normally not throw,
and doing so could be an indication that you have a problem with your
class.
>
Why?

/Peter

Jan 16 '07 #4
* Erik Wikström:
On Jan 14, 3:12 pm, "Grizlyk" <grizl...@yandex.ruwrote:
>Hello

I want to understand the exception habdling in C++ more, because i
think no one can apply anything free (free - without remembering large
unclear list of rules and exceptions from rules and exceptions from
exception from rules) if he does not understand "why" the thing have
done excactly as is.

I know, some people do not agree with me at the point of "why".

1.
I think, the exception is statical linknig, in other words, each throw
"knows" it appropriate catch during comile time.

Is it true?

Sounds logical to me
Consider:

#include <iostream>
#include <ostream>
#include <stdexcept>

void foo()
{
throw std::runtime_error( "Boo!" ); // Which catch?
}

void say( char const s[] ){ std::cout << s << std::endl; }

void a() { try{ foo(); }catch{...}{ say( "Argh!" ); } }
void b() { try( foo(); }catch{...}{ say( "Brgh!" ); } }

int main() { a(); b(); }

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 16 '07 #5

"Grizlyk" <gr******@yandex.ruwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
>
1.
I think, the exception is statical linknig, in other words, each throw
"knows" it appropriate catch during comile time.
Here's but just one way to implement exceptions:

Every catch-block generates an entry in one or more tables
that the compiler maintains internally. There is one table for
every type being caught anywhere, and the table contains
pointers to all such catch-blocks (perhaps coming from many
compiled object files, or even library).

When an object of some type is being thrown, the compiler
generates (or _statically_ links from run-time library!) code to
scan the table of catch-blocks for that type. The same code
also scans the stack, and for each stack frame, it either finds
a catch-block from the table (to which it then jumps), or if it
does not find one, it unwinds the current stack frame and
proceeds to the next one.

- Risto -
Jan 16 '07 #6
peter koch wrote:
But if i declare new try{} block inside destructor, can temporary
objects safe throw or can not and why?

A temporary object can throw, of course.
Does standard allow creation new try block during
"std::uncaught_exception()"?

Jan 16 '07 #7

Grizlyk skrev:
peter koch wrote:
But if i declare new try{} block inside destructor, can temporary
objects safe throw or can not and why?
A temporary object can throw, of course.

Does standard allow creation new try block during
"std::uncaught_exception()"?
Yes

Jan 16 '07 #8
Risto Lankinen wrote:
>
Every catch-block generates an entry in one or more tables
that the compiler maintains internally. There is one table for
every type being caught anywhere, and the table contains
pointers to all such catch-blocks (perhaps coming from many
compiled object files, or even library).

When an object of some type is being thrown, the compiler
generates (or _statically_ links from run-time library!) code to
scan the table of catch-blocks for that type. The same code
also scans the stack, and for each stack frame, it either finds
a catch-block from the table (to which it then jumps), or if it
does not find one, it unwinds the current stack frame and
proceeds to the next one.
the compiler generates code
(or _statically_ links from run-time library!)
Yes, can not be constant statical link, becasue function can be
declared in one module, but try block in another.

All other not clear.
Where can I read in inet more detailed description with screenshot of
stack's state at each step of execution of symple code example (for any
exception implementation)?

Jan 16 '07 #9

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

Similar topics

4
by: Jurko Gospodnetiæ | last post by:
Hi all. I was wondering. Can the standard basic_string<> c_str() member function throw any exceptions? Is it perhaps implementation dependent? I tried checking the standard and as far as I...
6
by: Arjen | last post by:
Hi, I'm reading the enterprise library documentation and there I see the throw statement. try { // run code } catch(Exception ex) {
6
by: MattC | last post by:
I noticed when storing large amounts of information in the StateServer Service that this does not increase in size, the worker process itself seems to grow. I set up a test case to try and prove...
4
by: Henning Makholm | last post by:
Having started in a job where I am to write Java code, I am working my way through the Java Language Specification. But the following situation gives me problems: public class Foo { public...
7
by: dick | last post by:
in the "try{throw}catch" structure, how the C++ code return the "type" thrown by a function?
6
by: Henryk | last post by:
I did a lot of delphi GUI programming recently. In my experience most of the time you just want to throw a standard exception with a descriptive message. Then all calling functions can handle...
6
by: Fir5tSight | last post by:
Hi, What does "throw" do? I understand that it throws an error when certain exceptional situation happens. My guess is that this *ignores* the error and *continues* so that it prevents the...
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
4
by: Elmo Watson | last post by:
I've got one main method, with a Try Catch block. In that, I catch the error and put it on a label. However, inside the Try portion, I also run another method. I have a Try/Catch block there,...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.