473,325 Members | 2,785 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,325 software developers and data experts.

How C++ Exception handling works ?

Hi All,

I have a doubt in the implementation of C++ try catch exception handling
procedure. Whenever there is a through, the execution has to go to the point
of catch. Now, if the catch block resides across DLL boundary (i.e. The
Catch block is there in another DLL). In that case the compiler can not put
the code for jumping to the catch point. What I think is that there needs to
be some explicit support from the OS that would maintain the try/catch block
in the kernel context and the compiler will get the jump context from the
OS. In that case a C++ compiler (Supporting exception handling) can't be
written for an OS which doesn't natively support C++.

I am totally confused and i have no clue how try/catch can be implemented.

Pls explain if anyone knows how it works.
Regards,
Sahoo
Jan 16 '06 #1
12 6716
* Subhransu Sekhar Sahoo:

I have a doubt in the implementation of C++ try catch exception handling
procedure. Whenever there is a through, the execution has to go to the point
of catch. Now, if the catch block resides across DLL boundary (i.e. The
Catch block is there in another DLL). In that case the compiler can not put
the code for jumping to the catch point.


Whatever made you think that a 'throw' is implemented as a direct jump?
'throw' is dynamic. Where it ends up depends on the call context.

--
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 '06 #2
Subhransu Sekhar Sahoo wrote:
Hi All,

I have a doubt in the implementation of C++ try catch exception handling
procedure. Whenever there is a through, the execution has to go to the point
of catch. Now, if the catch block resides across DLL boundary (i.e. The
Catch block is there in another DLL). In that case the compiler can not put
the code for jumping to the catch point. What I think is that there needs to
be some explicit support from the OS that would maintain the try/catch block
in the kernel context and the compiler will get the jump context from the
OS. In that case a C++ compiler (Supporting exception handling) can't be
written for an OS which doesn't natively support C++.

I am totally confused and i have no clue how try/catch can be implemented.

Pls explain if anyone knows how it works.


That does not answer your question, but you generally want
to avoid exceptions propagating across module boundaries altogether.

- J.
Jan 16 '06 #3
>That does not answer your question, but you generally want
to avoid exceptions propagating across module boundaries altogether. - J.


Is this a result of how compilers handle exceptions, or are there any
other reasons for doing so?
I am curious to know whether a C++ program knows about module
boundaries.

Jan 16 '06 #4
On Mon, 16 Jan 2006 06:16:02 GMT, "Subhransu Sekhar Sahoo"
<su***************@nokia.com> wrote:
I am totally confused and i have no clue how try/catch can be implemented.
Pls explain if anyone knows how it works.


http://www.codeproject.com/cpp/exceptionhandler.asp

Best wishes,
Roland Pibinger

Jan 16 '06 #5
Subhransu Sekhar Sahoo wrote:
Hi All,

I have a doubt in the implementation of C++ try catch exception handling
procedure. Whenever there is a through, the execution has to go to the point
of catch. Now, if the catch block resides across DLL boundary (i.e. The
Catch block is there in another DLL). In that case the compiler can not put
the code for jumping to the catch point. What I think is that there needs to
be some explicit support from the OS that would maintain the try/catch block
in the kernel context and the compiler will get the jump context from the
OS. In that case a C++ compiler (Supporting exception handling) can't be
written for an OS which doesn't natively support C++.

DLLs are Windows specific, maybe you would be better of asking in a VC++
group.

Every compiler has its own mechanism, so there is no standard explanation.

--
Ian Collins.
Jan 16 '06 #6
Shark wrote:
That does not answer your question, but you generally want
to avoid exceptions propagating across module boundaries altogether.


- J.

Is this a result of how compilers handle exceptions, or are there any
other reasons for doing so?
I am curious to know whether a C++ program knows about module
boundaries.

Standard C++ has no concept of module boundaries, it's a concept
specific to one OS.

--
Ian Collins.
Jan 16 '06 #7
Subhransu Sekhar Sahoo wrote:
Hi All,

I have a doubt in the implementation of C++ try catch exception handling
procedure. Whenever there is a through, the execution has to go to the point
of catch. Now, if the catch block resides across DLL boundary (i.e. The
Catch block is there in another DLL). In that case the compiler can not put
the code for jumping to the catch point. What I think is that there needs to
be some explicit support from the OS that would maintain the try/catch block
in the kernel context and the compiler will get the jump context from the
OS. In that case a C++ compiler (Supporting exception handling) can't be
written for an OS which doesn't natively support C++.

I am totally confused and i have no clue how try/catch can be implemented.

Pls explain if anyone knows how it works.
Regards,
Sahoo

How can the catch block reside across a DLL boundary? Anyway, this is an
implementation detail and has *nothing* to do with the language
support. Have you tried to test this assertion?
Jan 16 '06 #8

"Subhransu Sekhar Sahoo" <su***************@nokia.com> wrote in message
news:CS********************@news2.nokia.com...

I am totally confused and i have no clue how try/catch can be implemented.

Pls explain if anyone knows how it works.


There are many ways. Here's one in a nutshell:

To implement exception infrastructure, the compiler creates
an exception record for every function (and statement block
and often also for a "virtual block" which is when a complex
declaration appears in the middle of a statement block and
therefore requires an augmentation to the cleanup routine) that
it compiles. This record contains pointers to the beginning
and end of each block's code, and a pointer to the code that
cleans up the stack when exiting the block.

To compile "catch", a new field is added to the innermost
containing exception record that indicates the entry point of
the catch routine, and a descriptor which encodes the type
of exception object being caught.

To compile "throw", a run-time library routine will be called
that [1] locates the active exception record (this can be done
because the location of "throw" will be within the beginning-of
and end-of addresses of exactly one function), [2] sees if that
record has a catch-handler for the thrown type and depending
on that, [3a] calls the catch handler, or [3b] calls the block's
stack cleanup code and restarts from [=>1] using the next
highest return address in the stack as the "location of throw".

Brief consideration of DLL's [otherwise a non-C++ topic]:
The throw-handler is necessarily on a "lower" level than the
application code. For a stand-alone application the minimum
level is run-time library. For a dynamically linked application,
it is necessary to put the handler into the same level with the
module loader and give it access (perhaps thru import/export)
to all modules' exception records.

- Risto -
Jan 16 '06 #9
>Standard C++ has no concept of module boundaries, it's a concept
specific to one OS.


What about cases in which you mix C and C++ code? C++ runtime libraries
are different from C-specific (I could be wrong), but you can treat
them as two different entities equvalent to modules. Lets say a C
program calls a C++ function, and this C++ function is designed to
throw an exception!

Jan 16 '06 #10
Shark wrote:
Standard C++ has no concept of module boundaries, it's a concept
specific to one OS.

What about cases in which you mix C and C++ code? C++ runtime libraries
are different from C-specific (I could be wrong), but you can treat
them as two different entities equvalent to modules. Lets say a C
program calls a C++ function, and this C++ function is designed to
throw an exception!

If it was designed to throw exceptions, then it shouldn't be given
extern "C" linkage. But I see your point, an exception could bubble up
from within the C++ code.

I'd guess we are straying into undefined behaviour here, seeing as C has
no concept of exceptions. Some exception implementations would work in
a C++ calls C which calls C++ situation, others would not.

Interesting side question though: should extern "C" implicitly add
throw() to the prototype thus triggering unhanded if an exception is thrown?

--
Ian Collins.
Jan 16 '06 #11
Roland Pibinger wrote:
On Mon, 16 Jan 2006 06:16:02 GMT, "Subhransu Sekhar Sahoo"
<su***************@nokia.com> wrote:
I am totally confused and i have no clue how try/catch can be implemented.
Pls explain if anyone knows how it works.


http://www.codeproject.com/cpp/exceptionhandler.asp

Best wishes,
Roland Pibinger


An interesting article - if you're using MS VC++ on Windows...

Regards,
Larry
Jan 16 '06 #12
Ian Collins wrote:
Shark wrote:
That does not answer your question, but you generally want
to avoid exceptions propagating across module boundaries altogether.

Is this a result of how compilers handle exceptions, or are there any
other reasons for doing so?
I am curious to know whether a C++ program knows about module
boundaries.

Standard C++ has no concept of module boundaries, it's a concept
specific to one OS.


What I meant to say (or rather to quote) related to modules
in a "programming engineering" sense. I was just reading this
Sutter/Alexandrescu book and Rule#62 said
"Don't let exceptions propagate across module boundaries".

I can back-translate to English the synopsis of this for you:

"You shouldn't throw pebbles into your neighbour's garden.
There is no such thing as a binary level standard of
exception handling. Therefore you should not let exceptions
propagate across modules, unless you are able to make sure
that all compiler 'set and setting' are identical for both
parts. If you can't guarantee that, the modules may handle
exceptions in a non-interoperable manner and there will be
no monolithic way of propagating exceptions. Therefore it
should be stressed that "thou shalt not allow for exceptions
to propagate across modules and subsystems"".

For more info try this book, it will be called "C++ coding
standards" or something similar (again, I only have a translation).
It was recommended by BS himself.

HTH,
- J.
Jan 17 '06 #13

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...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
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...
9
by: sam | last post by:
Hi, I m using BSD stl with g++ compiler. The following code dos not throw exception if the input file is not exist: try { fin.open(f, ios::in); } catch (std::exception& e)
7
by: Dan Bass | last post by:
In a somewhat complex application, I've developed plug-in architecture and am having a problem as to when to catch general exceptions for logging purposes. In each plug-in class library, for...
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...
1
by: Noor | last post by:
Hi all, I am trying to catch all types of exceptions from a app regardless of whether it is in debugger mode( VS development environment) or run the.exe file outside the IDE. My App...
6
by: Nick Reeves | last post by:
While developing the below code I noticed that exceptions were not working as expected. An exception is thrown inside an XML import DLL and not being caught inside the Main() code fragment,...
4
by: Peter Aitken | last post by:
It seems that some classes have their own exception handling built in. For example if I try to use FileStream to open a file that does not exist, my Try...Catch block seems to be ignored and...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.