I happened to see an old video on channel 9 where Loius and Ale are discussing exception handling in details. This is a real informative as they discuss how exception handling is done in Win32 and 64 bit, why it is done so, cost of exception handling and usage, exception across DLLs and modules.
Ale Contenti and Louis Lafreniere: Understanding Exceptions and When/How to Handle Them
Video link: http://channel9.msdn.com/showpost.aspx?postid=343189
During the video I captured some notes and putting it here without any formatting. So look this to get basic idea but spend an hr to listen to them.
They mention blog post by Chris Brumme on exception handling which is _the_ informative dissection on the subject. Post is here.
Notes:
Exceptions generated in C++, machine level (SEH) and generated by code via throw. Performance impact varies a lot depending on whether user wants to catch all exceptions or only program generated (throw).
5.00 - Should catch everything at the top level. Catch and then die. C++ runtime actually catches all (SEH and throw) the exceptions. User should not put catch (…) in main but let it pass to CRT and let it handle.
7.30 Anatomy of exception. Two pass handling of an exception.
void f()
{
throw std::outof.range()
}
void g()
{
try {}
except(0) {}
}
void h()
{
try
{
g()
}
catch (const std::outof.range& e)
{}
}
int main()
{
h()
}
stack:
f ; search handler, nothing so go down
g ; except(0) - filter
h
main
13.00 (time)
Two pass:
1. Look for handler..
2. If handled then go back and call destructor
if no handler then CRT will call terminate.
compiler swithc /EHs - does not run any destructors and dies immediately.
24.40[time]
Performance in exception handling.
Win32: How exception handling is implemented (SEH level)
void f()
{
Obj obj;
throw std::outof.range();
}
How OS keep track of exceptions:: using linked list at FS:0 keeps track of all frames which has exception. Need to link and unlink pointers at FS:0 everything we enter f(). Since f() has created obj, it needs to be destroyed. Keep track of state from -1 up and down based on obj creation and destructor (this has some overhead). If we can figure out which obj don’t need exception handling and then don’t keep track of state of that object.
33:30[time]
What is the right thing to do?
40time]: Current compiler framework is mainly C and new Phoneix compiler is in C++ with very minimal exception throwing - when compiles throws up hands.
47[time]: Throwing exception across DLL is not a good idea. It is tricky and harder to handel.
50[time]: Destructor should never throw. If during exception handling (unwinding) if destructor throws then program terminates.