In C++, exceptions are often faster than manual error checking when errors are rare.
But C++ does not provide a stack trace.
Re:You’re better off using Exceptions
by safety-second on Monday February 03, 2020 @02:37PM
(#22228074)
C++ exceptions are only good if you use them like signals from C or panics in Go. This means you have to use error values for 99% of errors or you lose this benefit. The fact that the STL sprinkles exceptions everywhere doesn't help this.
C++ exceptions are so non-deterministic and slow that every real-time system disables them just to be sure.
> But C++ does not provide a stack trace
Which makes exception in C++ a very, very bad idea!
My reaction to 'the test fails because map::at threw an exception': stupid STL!!
A core dump would be so much easier to analyze.. Gdb's 'catch thow' is wonderful (well, it is after I fixed the part of our codebase which use exceptions as a control flow mechanism)
> My reaction to 'the test fails because map::at threw an exception': stupid STL!!
> A core dump would be so much easier to analyze..
Uncaught exceptions call terminate(), which by default calls abort(), which generates a core dump. So if you want a core dump, just avoid catching the exception.
Except that I didn't put all these "catch"..
But you're right, that's what should be done (or at least, rethrowing the exception from the catch block).
Re:You’re better off using Exceptions
by AstralStorm on Tuesday February 04, 2020 @11:57AM
(#22237897)
C++ can have a stack trace, there are enough libraries providing this functionality and even compiler extensions. Say, libunwind, though you will have to demangle names.
And a debugger will see the stack as it is.
Also works in C.
Re:You’re better off using Exceptions
by iainmerrick on Monday February 03, 2020 @02:48PM
(#22228191)
You don’t always get a stack trace, but it still has to unwind the stack, calling destructors as needed, and check the exception’s type against each catch block.
Hence 'when errors are rare'.
Re:You’re better off using Exceptions
by iainmerrick on Monday February 03, 2020 @02:59PM
(#22228303)
I guess we’re violently agreeing? Exceptions are an order of magnitude (or several orders) slower than function calls and returns; but that can still be efficient overall, if only a tiny fraction of calls fail.
I’d simply add that most people greatly underestimate how expensive exceptions are, even in C++.
Re:You’re better off using Exceptions
by AstralStorm on Tuesday February 04, 2020 @12:00PM
(#22237930)
Calling the destructors is identical to ending any scope in C++, including function calls. Where is that extra expense?
Re:You’re better off using Exceptions
by iainmerrick on Tuesday February 04, 2020 @12:33PM
(#22238370)
You likely have to parse exception blocks and maybe chase pointers to find the objects to destroy. In the happy path, you can just call the destructors directly.
Maybe not Python? But Python is slow regardless.