SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
dirtydroog
Posts: 368
Joined: July 12th, 2007, 6:32 pm

Exception handling in C++

August 13th, 2007, 2:06 pm

QuoteOriginally posted by: JimQuoteQuote--------------------------------------------------------------------------------When an object's constructor throws an exception its destructor will not be called so the onus is on you to perform any cleanup.--------------------------------------------------------------------------------How exactly? You don't know the point of failure so you don't know what needs to be cleaned up. By separating the construction ofthe object from the construction of it's member variables you do know this... i.e. the throw was caused by OOM for the object and thusnone of the member variables were initialised.Is the above coded incorrectly? Help me out here people. Hold on here, guys.When an exception is thrown in a constructor, the destructor for that class is not called, but that does not mean that no desctructor is called. Any properly constructed base classes or member variables which have be constructed up to the point of the exception being thrown DO have their destructors called. Therefore, if one were to use auto_ptr's or the RAII idiom, those resources get properly released.Are you sure. When I tried to code I pasted below out yesterday (VC++ Express) no destructors were called.I'll check it again this evening.
 
User avatar
Jim
Posts: 340
Joined: February 1st, 2002, 5:20 pm

Exception handling in C++

August 13th, 2007, 2:56 pm

QuoteAre you sure.Well, I am sure what the C++ standards says, which is what I relayed to you. I cannot be sure whether the compiler you are using conforms to the standard.QuoteWhen I tried to code I pasted below out yesterday (VC++ Express) no destructors were called.That is because your B class contains a raw pointer to an A, not an auto_ptr. A raw pointer is not an object and has no desctructor to call. An auto_ptr, on the other hand, is an instance of a class template and would have its destructor called (which, in turn, would do a delete on its internal raw pointer).QuoteI'll check it again this evening. Thank you.By the way, real C++ programmers use iostreams, like cout, not the C function printf.
 
User avatar
dirtydroog
Posts: 368
Joined: July 12th, 2007, 6:32 pm

Exception handling in C++

August 13th, 2007, 3:23 pm

QuoteOriginally posted by: JimQuoteBy the way, real C++ programmers use iostreams, like cout, not the C function printf.Ha ha, real C++ programmers use JTAG debuggers, not stdout.
Last edited by dirtydroog on August 12th, 2007, 10:00 pm, edited 1 time in total.
 
User avatar
INFIDEL
Posts: 749
Joined: November 29th, 2005, 4:17 pm

Exception handling in C++

August 13th, 2007, 4:42 pm

Quoteyurakm: Actually, the code often is somewhat smaller, especially if you have several returns in functions.I've heard the claim that coding proper exception handling actually results in shorter code generated behind the scenes by the compiler, because it then doesn't have to worry about all the possible things that can go wrong. Does anybody know this for a fact?
 
User avatar
Jim
Posts: 340
Joined: February 1st, 2002, 5:20 pm

Exception handling in C++

August 13th, 2007, 4:45 pm

QuoteHa ha, real C++ programmers use JTAG debuggers, not stdoutNo, stdout is a C language artifact. In C++ we use cout.Just come out and admit it -- you're a C programmer using a C++ compiler for your code, but you use, maybe, 10% of the features which distinguish C++ from C (and exceptions, the topic of this thread, aren't in the 10%.) But don't worry, you're not alone. The vast majority of quants and quant developers are really C programmers who know just enough C++ to be dangerous.And, by the way, 25 years ago I was doing embedded programming, using an ICE to debug a mix of C and 8085 assembly code. JTAG is cake compared to tools available 25 years ago.
 
User avatar
INFIDEL
Posts: 749
Joined: November 29th, 2005, 4:17 pm

Exception handling in C++

August 13th, 2007, 5:04 pm

QuoteNote that C++ compilers, including g++, generate alot of exceptioncode even if you never throw or catch an exception! This is becauseC++ is required to call destructors even if some called method orfunction throws an exception. Thus, each and every destructor mustget wrapped into the equivalent of a `finally' clause just in case oneof your methods throws an exception.Here.
 
User avatar
dirtydroog
Posts: 368
Joined: July 12th, 2007, 6:32 pm

Exception handling in C++

August 13th, 2007, 5:05 pm

QuoteOriginally posted by: JimQuoteHa ha, real C++ programmers use JTAG debuggers, not stdoutNo, stdout is a C language artifact. In C++ we use cout.Just come out and admit it -- you're a C programmer using a C++ compiler for your code, but you use, maybe, 10% of the features which distinguish C++ from C (and exceptions, the topic of this thread, aren't in the 10%.) But don't worry, you're not alone. The vast majority of quants and quant developers are really C programmers who know just enough C++ to be dangerous.And, by the way, 25 years ago I was doing embedded programming, using an ICE to debug a mix of C and 8085 assembly code. JTAG is cake compared to tools available 25 years ago.I written here before that I prefer C to C++. However, my job is completely C++, perhaps we only use the 10% of it becauseby the time the company started to use C++, hardly any of the features that the language has today existed then. And whenyou write code that other companies buy and build upon, you can't go changing things will-nilly - you piss off your customers.8085? hardcore. Anyway, changing the ctor to X(bool aThrow) : iA(NULL), iB(NULL) { auto_ptr<X> me(this); // Sneaky sneaky to get free X's memory on ctor failure. No need for other auto_ptrs iA = new A(aThrow); iB = new A(!aThrow); me.release(); } ~X() { delete iA; delete iB; }does what I expect. So, throwing from constructors... bad unless you use auto_ptr. Awesome.
Last edited by dirtydroog on August 13th, 2007, 10:00 pm, edited 1 time in total.
 
User avatar
mj
Posts: 3449
Joined: December 20th, 2001, 12:32 pm

Exception handling in C++

August 15th, 2007, 10:32 pm

I always practice the "rule of almost zero" : no user defined copy constructor or assignment operator, destructor only defined in order to make it virtual but always trivial. All memory deletion handled by smart pointers, throwing from a constructor then really isn't a problem.
 
User avatar
Polter
Posts: 2526
Joined: April 29th, 2008, 4:55 pm

Exception handling in C++

November 17th, 2015, 7:21 pm

Handling Disappointment in C++QuoteWhen a function fails to do what we want, we are disappointed. How do we report that disappointment to callers? How do we handle that disappointment in the caller?In the discussion of a couple of new approaches to handling disappointment, the Evolution Working Group wanted general advice to programmers on how to answer those questions for their application. This paper provides that advice.Kinds of DisappointmentsQuoteThere are many kinds of disappointments and programmers will want to report and handle them differently.- The system implementation failed, e.g. out of memory.- Something passed to the function failed, e.g. an exception thrown from a callback.- The function detected an inappropriate environment, e.g. null pointer parameter.- Data structure bounds prevent completion, e.g. pushing a full queue.- The disappointment is expected, e.g. popping an empty queue.Of these examples, the last two are not errors. Hence, we use the term disappointment instead of error.. . .Recommendation to ProgrammersQuoteProgrammers of a function should consider how to communicate disappointment to their callers with the following advice, taken in order.- If the function will never disappoint, use noexcept and do not have a status.- If there are significant real-time constraints on the program, where the cost of an exception is prohibitive, use a status-based function. In such programs, a strong review program will be necessary to ensure that all disappointments are handled. This path will prohibit some types and programs.- If a disappointment not known in advance, throw an exception. Programmers cannot handle what they cannot know. The most common instance of this case is a function invoking a callback function that throws. The callback function may throw exceptions unknown to the outer function.- If a disappointment is rare and not actionable, throw an exception. The concurrent queue returns a status from most functions, but no status includes a mutex failure. When a mutex fails, the queue operations will throw an exception.- If the function may provide advisory information, use status_value. Callers can decide whether a disappointment is rare or actionable.- If the exceptional object is expensive to construct, use expected. Callers can decide whether a disappointment is rare or actionable. (The exceptional object need/should not have a 'success' value, as the programmer has access only to the exceptional object on success.)- Otherwise, use status_value or expected based on which seems more natural for code explicitly handling disappointment.https://github.com/ptal/expectedhttp:// ... d-proposal
Last edited by Polter on November 16th, 2015, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 61537
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Exception handling in C++

November 17th, 2015, 7:33 pm

Bertrand Meyer would call it "design by contract"?
Last edited by Cuchulainn on November 17th, 2015, 11: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
Cuchulainn
Posts: 61537
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Exception handling in C++

November 17th, 2015, 7:55 pm

Here is a good test: how would disappointments subsume what engineers call defects, fault, error, failure? - The system implementation failed, e.g. out of memory.- Something passed to the function failed, e.g. an exception thrown from a callback. - The function detected an inappropriate environment, e.g. null pointer parameter.- Data structure bounds prevent completion, e.g. pushing a full queue. - The disappointment is expected, e.g. popping an empty queue. Meyer's contracts subsume these, no?
Last edited by Cuchulainn on November 16th, 2015, 11: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
Cuchulainn
Posts: 61537
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Exception handling in C++

November 24th, 2015, 7:31 am

Here's a question for library developers e.g. booste.g. the code for the cdf(x) of a normal distribution N(a,b) looks like1. Check a2. Check b3. is x infinite4. check x infinite (again), a problem with layered software i.e. duplicated checking.5. Call erfcThis Maginot line defence is not needed when the client knows for sure that a,b and x are correct. Secondly, computing the cdf() for an array of x will be inefficient because of redundant checking.Has anyone addressed/thought of/solved this problem? // In some designs state machines are used to make the design customizable. you have 1 state machine with Maginot line checking and another that doe no checking.
Last edited by Cuchulainn on November 23rd, 2015, 11: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
Cuchulainn
Posts: 61537
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Exception handling in C++

November 24th, 2015, 8:09 am

QuoteOriginally posted by: outrunQuoteOriginally posted by: Cuchulainn<// In some designs state machines are used to make the design customizable. you have 1 state machine with Maginot line checking and another that doe no checking.For implementations.. in C++.. you can use policies to inject behaviour. These are compile-time configurations that don't have an run-time overhead.That's nice. So, we could have a version that does not have checks 1-4.But Boost Maths does not have it? Are you saying all those check() become empty? Q long story short: you just define blank check(s)() to ensure no error checking? However, in the current case this is not obvious to me. Just to take a niity griity example, I don't see how detail::check_x() can be empty.
Last edited by Cuchulainn on November 23rd, 2015, 11: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
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