SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
Sonyah
Topic Author
Posts: 153
Joined: December 11th, 2006, 3:58 pm

Exception handling in C++

May 25th, 2007, 1:22 pm

Can anyone recommend a good book chapter or references about this?I would like to know a bit more about good design of C++ exception handlers.
 
User avatar
quantmeh
Posts: 5974
Joined: April 6th, 2007, 1:39 pm

Exception handling in C++

May 25th, 2007, 1:41 pm

check out Stroustrup's Design and Evolution of C++, he gives a motivation why exactly it was done the way it's done
 
User avatar
DominicConnor
Posts: 11684
Joined: July 14th, 2002, 3:00 am

Exception handling in C++

May 25th, 2007, 7:02 pm

D&E is good, but not perhaps your first book.I tend to recommend Herb Sutters books, exceptional C++ and more exceptional C++http://books.global-investor.com/books/ ... dd749630f0
 
User avatar
helix
Posts: 63
Joined: May 23rd, 2005, 3:07 pm

Exception handling in C++

May 27th, 2007, 2:14 pm

Stan Lippman's C++ Primer provides a nice introduction. I agree with DCFC, Herb Sutter's books are great. I'd also recommend Scott Meyers books:http://www.amazon.com/exec/obidos/searc ... t%20Meyers
Last edited by helix on May 26th, 2007, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 61593
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Exception handling in C++

May 27th, 2007, 11:55 pm

The design of exceptions introduces all kinds of new challenges; it's not as straightforward as it seems: Tom Cargill, C++ Report, Volume 6, Number 9, November-December 1994 QuoteCounter-intuitively, the hard part of coding exceptions is not the explicit throws and catches. The really hard part of using exceptions is to write all the intervening code in such a way that an arbitrary exception can propagate from its throw site to its handler, arriving safely and without damaging other parts of the program along the way. Correctness of the code after exceptions have been introduced is key. Stroustrup 1997 is the standard reference.
Last edited by Cuchulainn on May 27th, 2007, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget
 
User avatar
Sonyah
Topic Author
Posts: 153
Joined: December 11th, 2006, 3:58 pm

Exception handling in C++

May 29th, 2007, 9:48 am

Thanks everyone for references and links. My impression is that it would not be that difficult to live without try/catch/throw blocks - just using return codes instead. So would be interested to hear opinions!
 
User avatar
DominicConnor
Posts: 11684
Joined: July 14th, 2002, 3:00 am

Exception handling in C++

May 29th, 2007, 10:28 am

You can't do without exceptions unless you're prepared to cut yourself off from quite a swathe of C++That's why there's a mini lecture on alternative error management strategies and a whole one on "Cruel and Unusual Pointers" on the CQF.C&UP tells you about auto_ptr, shared_ptr etc, and this leads to object lifetime mechanism like the singleton pattern.We also deal with the worst bug in the world, a variant of which was given that title by IBM & Microsoft when we were all friends and built operating systems together.Why you can't just have return codes (short version);With operators like [] there isn't anywhere to put the return code.The grown up class libraries like Boost and STL use them. MFC doesn't really , and yes the story about me threatening MFC's author with a knife is mostly true.Operator new throws exceptions, fuck me if I know how you deal with that by return codes (OK, I do know how, but I cut that bit from the CQF C++ as being too Cruel and unusual.)It requires the coder using the function to remember to check every return code. Yeah, reckon that happens a lot, and Britney Spears has offered me her body if I debug some of her floating point code.But let us look at the worst bug in the world.The most common way of returning an error is demonstrated in malloc, which either returns 0 or a pointer to newly allocated memory.ie you use an "impossible" value.So how do you deal with numeric function that gets passed "wrong" values ?Yep, you pass back another "wrong" value.But as above, it's not always checked, and it's "obviously wrong value, can get sucked into a calculation that make it right.Try tracing that one down.Even the pointer shit can blow up in your face.double * a = reinterpret_cast<double *> malloc(sizeof (double) *100);*a = 42;Will die, butdouble *pa = a+ index;*pa = 42;May die, it may not will cause corruption, or not.
Last edited by DominicConnor on May 28th, 2007, 10:00 pm, edited 1 time in total.
 
User avatar
quantmeh
Posts: 5974
Joined: April 6th, 2007, 1:39 pm

Exception handling in C++

May 29th, 2007, 12:23 pm

QuoteOriginally posted by: SonyahMy impression is that it would not be that difficult to live without try/catch/throw blocks - just using return codes instead. can u use return codes from constructors?exceptions have their use in programming. sometimes they're overused (like in Java often), but it's not the same as using return codes. i would suggest not to get overwelmed by a thorough and deep coverage of exceptions in the beginning. at first, simply learn how to use them syntaxically. that way it's easier. after some time u may go and read conceptual stuff. i did it the other way, but there were no exceptions in C++ at that time, anyways
 
User avatar
Sonyah
Topic Author
Posts: 153
Joined: December 11th, 2006, 3:58 pm

Exception handling in C++

May 31st, 2007, 7:59 am

Thanks alot everyone for replies! Exceptionally interesting!Sonyah
 
User avatar
fizik
Posts: 23
Joined: January 10th, 2006, 3:56 am

Exception handling in C++

June 14th, 2007, 1:34 pm

QuoteOriginally posted by: DCFCWith operators like [] there isn't anywhere to put the return code.The grown up class libraries like Boost and STL use them. MFC doesn't really , and yes the story about me threatening MFC's author with a knife is mostly true.Operator new throws exceptions, fuck me if I know how you deal with that by return codes (OK, I do know how, but I cut that bit from the CQF C++ as being too Cruel and unusual.)It requires the coder using the function to remember to check every return code. Yeah, reckon that happens a lot, and Britney Spears has offered me her body if I debug some of her floating point code.This is an impressive example of explicit prose from DCFC I have definitely heard a lot of that from programmers. I wonder how much it is a part of the quant work culture? Would the mastery of it count as a presentation skill, for instance?
 
User avatar
Cuchulainn
Posts: 61593
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Exception handling in C++

June 14th, 2007, 3:14 pm

QuoteThis is an impressive example of explicit prose from DCFC Listen and learn.
Last edited by Cuchulainn on June 13th, 2007, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget
 
User avatar
lballabio
Posts: 983
Joined: January 19th, 2004, 12:34 pm

Exception handling in C++

June 15th, 2007, 7:36 am

 
User avatar
Sonyah
Topic Author
Posts: 153
Joined: December 11th, 2006, 3:58 pm

Exception handling in C++

June 15th, 2007, 2:31 pm

Great links! Thanks alot Luigi!
 
User avatar
dirtydroog
Posts: 368
Joined: July 12th, 2007, 6:32 pm

Exception handling in C++

August 10th, 2007, 3:14 pm

QuoteOriginally posted by: jawabeanQuoteOriginally posted by: SonyahMy impression is that it would not be that difficult to live without try/catch/throw blocks - just using return codes instead. can u use return codes from constructors?exceptions have their use in programming. sometimes they're overused (like in Java often), but it's not the same as using return codes. i would suggest not to get overwelmed by a thorough and deep coverage of exceptions in the beginning. at first, simply learn how to use them syntaxically. that way it's easier. after some time u may go and read conceptual stuff. i did it the other way, but there were no exceptions in C++ at that time, anyways My workplace's coding standards state that constructor should never contain code that can 'fail'.Instead we have a static function that creates the object for you, constructor is private.Widget* Widget::New(){Widget* w = new Widget();w->Construct(); // Put code that can fail herereturn w;}void main(){try {Widget* w = Widget::New();delete w;} catch(...) {}}Throwing an exception from a constructor is quite bad practice.The 'new' operator can also be overloaded so as to return null rather than throw an exception on allocation failure.My own take on it is the lower the level of the code the less you should use exceptions.
 
User avatar
Jim
Posts: 340
Joined: February 1st, 2002, 5:20 pm

Exception handling in C++

August 10th, 2007, 8:37 pm

QuoteThrowing an exception from a constructor is quite bad practice.I strongly disagree. Go read some of the books by Sutter or Meyers to understand why.QuoteMy own take on it is the lower the level of the code the less you should use exceptions. Bzzzzt. Wrong answer. The whole idea behind exceptions is to separate the detection of an error and the handling of the error. Often the low level code will detect the error (for example, like a socket connection being dropped) but does not have the requisite information about what to do about the error (retry connecting, or stopping with an error message, ...). By throwing the exception the low level code informs higher level code of the problem; the higher level code (which is closer to the user) does the error handling. With exceptions, all the intermediate code between the low level and the high level doesn't have to pass some some sort of return code, the higher level code doesn't have to keep checking return codes (or forget to check return codes), and the exceptions can contain numerous pieces of information regarding the error without resorting to log files or storage in global variables.QuoteMy workplace's coding standards state that constructor should never contain code that can 'fail'.That's your problem, not mine. My preference is to construct an object in a valid state (and never let it leave a valid state) or don't construct it at all. That way I never have to ask an object "Are you valid?" before I use it. Allocating an object and then making it "Construct" is just another form of "C" code doing a malloc followed by an initialization. Not C++ in my book.
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On