Skip to main content
gamedev.net
🔒 Locked

Exceptions

Started by walkingcarcass May 11, 2003 at 11:30 AM 22 replies 1.1k views
Original Post
walkingcarcass
walkingcarcass
In an exception handler, how can I get the address of the instruction which raised an exception? I''m not afraid of using assembly here. ******** A Problem Worthy of Attack Proves It''s Worth by Fighting Back
spraff.net: don't laugh, I'm still just starting...
dede
dede
You can''t really, but you can number each exception. (assuming you are rolling your own), and then tell that way. Its not end-user friendly, but it can be useful.
walkingcarcass
walkingcarcass
The exception handler must have some mechanism to return to the code if the exception has been accomodated. I recall something about a chain of data structures and the FS register...?

********


A Problem Worthy of Attack
Proves It''s Worth by Fighting Back
spraff.net: don't laugh, I'm still just starting...
risingdragon3
risingdragon3
There is no such thing, in C++. Handling does NOT go back to the point of exception after the exception handler is finished.

I dunno about Win32 Structured Exceptions though.
Thunder_Hawk
Thunder_Hawk
You shouldn't really have a problem with this if you are saving exceptions for fatal sorts of problems (like running out of memory). If you're looking for the line number for debugging purposes, you can pull it off with a little simple macro work

[edited by - Thunder_Hawk on May 11, 2003 3:37:32 PM]
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
walkingcarcass
walkingcarcass
If execution doesn''t resume after an exception, how can the program possibly continue running?

********


A Problem Worthy of Attack
Proves It''s Worth by Fighting Back
spraff.net: don't laugh, I'm still just starting...
antareus
antareus
quote:
Original post by walkingcarcass
If execution doesn''t resume after an exception, how can the program possibly continue running?


When an exception is thrown, the stack unwinds and the exception handlers are run on the exception until it either:
1. unwinds through main, at which point you get an "unhandled exception" dialog box and the program forcibly terminates.
2. reaches a handler that does not rethrow the exception. Execution continues after this handler as usual.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
walkingcarcass
walkingcarcass
You mean the code immediately after in memory, like the exception doesn''t have a ret? WOAH! Red flag! I can imagine abandoning a function halfway through causing some really nasty problems, corruptions, leaks etc


what *exactly* (in micro-detail) is the sequence of events when an exception is thrown?

********


A Problem Worthy of Attack
Proves It''s Worth by Fighting Back
spraff.net: don't laugh, I'm still just starting...
Jan Wassenberg
Jan Wassenberg
fs points to the thread (info | environment) block; its first field is a pointer to the head of the exception handler chain.
fs -> tib
tib[0] -> head
head[0] -> new handler
head[1] -> old handler(s)
This handler receives an EXCEPTION_RECORD*, which tells you the address of the instruction that bombed.
Return value specifies continue, next handler, or default.

We need to differentiate between SEH and C++ exceptions. As soon as your code causes a processor exception to be raised, the Windows handler gets control. It examines the handler list, and calls them sequentially (unless one decides to continue execution, in which case the instruction is restarted).
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
Austrian Coder
Austrian Coder
Removed!

[edited by - Austrian Coder on May 11, 2003 4:43:10 PM]
antareus
antareus
quote:
Original post by walkingcarcass
I can imagine abandoning a function halfway through causing some really nasty problems, corruptions, leaks etc


Destructors are run as the stack unwinds. Use resource acquisition is initialization idiom for resources whenever possible, and std::auto_ptr for those you use operator new on.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
walkingcarcass
walkingcarcass
forget deconstructors, auto pointers etc, if my exception handler can "fix" the problem (eg divide by zero sets FPU stack top to very a big number) then how can execution continue from the bad instruction? Is there a danger of screwing up some kind of exeption handler stack?

********


A Problem Worthy of Attack
Proves It''s Worth by Fighting Back
spraff.net: don't laugh, I'm still just starting...
bjmumblingmiles
bjmumblingmiles
You''re trying to redirect the divide by zero error? Thats only for integers anyway, and you cant catch that kind of exception like that...but floats do not have the same problem because there isnt a float 0. You can usually only catch non-syntactical errors, like something just didnt work, not memory read errors etc.

Brian J
Brian J
Qw3r7yU10p!
Qw3r7yU10p!
Herb Sutter wrote the books ''Exceptional C++'' and ''More Exceptional C++'' based on his online column ''Guru of the Week''. They are articles that pose a question and then a few days, weeks or months later (after much discussion on comp.lang.c++.moderated) he will post a comprehensive answer. They look at all the tough areas of C++, language features and design issues etc. If you do a text search on that page for ''exception'' you''ll find about 10 or 12 articles that deal with exception issues. Here are just a few that might get you going.

quote:
Code Complexity - Part I
Difficulty: 9 / 10

This problem presents an interesting challenge: How many execution paths can there be in a simple three-line function? The answer will almost certainly surprise you.



quote:

Try and Catch Me
Difficulty: 3 / 10

Is exception safety all about writing try and catch in the right places? If not, then what? And what kinds of things should you consider when developing an exception safety policy for your software?



quote:

Constructor Failures
Difficulty: 7 / 10

What exactly happens when a constructor emits an exception? What if the exception comes from an attempt to construct a subobject or member object? This issue of GotW analyzes one aspect of C++ in detail, shows why it should work the way that it does, and demonstrates the implications for constructor exception specifications.


walkingcarcass
walkingcarcass
This is not a C++ speific question. When something goes wrong and the processor detects an exception, it goes through the OS defined process of calling exception handlers until a suitable one is found, right?

Once execution enters an exception handler, what information is a vailable and how does the program continue.

********


A Problem Worthy of Attack
Proves It''s Worth by Fighting Back
spraff.net: don't laugh, I'm still just starting...
Kylotan
Kylotan
quote:
Original post by walkingcarcass
This is not a C++ specific question.

If you''d said that in your first post - or answered dalleboy when he asked you about this explicitly, this thread would have made a lot more sense and wasted fewer people''s time.

You''ve not even specified what processor you''re talking about (although I suppose we can probably assume Intel x86).


[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
walkingcarcass
walkingcarcass
Are exceptions themselves C++ specific? No! Is it my fault you assumed it was a C++ question? No! Was there any hint in the question that I meant C++? No!

I don''t want to sart flaming here, but it''s a bit precious to accuse me of wasting time.

The original question is simple, direct, and unambiguous.

********


A Problem Worthy of Attack
Proves It''s Worth by Fighting Back
spraff.net: don't laugh, I'm still just starting...
Arild Fines
Arild Fines
Unlike more sophisticated languages, C++ doesn''t allow you to retrieve a stack trace from the exception object.

...and that is why I believe linux must be destroyed.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Kylotan
Kylotan
quote:
Original post by walkingcarcass
Are exceptions themselves C++ specific? No! Is it my fault you assumed it was a C++ question? No! Was there any hint in the question that I meant C++? No!

How are exceptions used by most people on these boards? As programming language features. What programming language is used the most here? C++. So that is a very reasonable assumption for people to make given that the scope for potential answers is massive without you providing more information. Yet, you ignored the guy who tried to help refine your question right at the start. And have you not still refused to even specify what processor you want to do this on? If you want answers, ask a decent question in the first place, and try helping those who try to help you.

quote:
The original question is simple, direct, and unambiguous.

The original question may well be simple and direct, but it is also wholly ambiguous. The answer you would receive depends entirely on your definition of ''exception''. Which is why Dalleboy kindly took the time to ask you to elaborate.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.