From my personal experience you should identify all the places when division by zero can occurred and handle error separately.I agree with you. However the DEBUG mode serves to capture unexpected situations, and this is to add more power to DEBUG mode detection of unexpected and nasty events. In addition to numerical errors, I have included unhandled exceptions, pure virtual functions and invalid parameter handler.
In addition to catching signals I would also print the stack trace automatically (and eventually write automatically to a file)Did you mean a minidump file? it is very easy to get, but I did not include it because I did not know how to analise it.]]>
Influential people think that divisions by zero or negative square roots are admissible in a good code. I do not agree.
static CrashHandler crash;
/** * PUBLIC API DOCUMENTATION * In case when divider is 0 std::runtime_error is thrown. */ int Calculate(int divider) { if (divider == 0) { throw std::runtime_error("Calculate(): Divider can not be zero..."); } return 20 / divider; } // Now the client could deiced what to do with the exception - it can be handled locally or globally in main function, but it is still the consumer choice what to do with it // In case of global handler provided by library the decisions were taken from application creator. // ... Below line may even decrese the performance, because on each new allocation NewHandler will be called. std::set_new_handler(NewHandler); std::set_unexpected(UnexpectedHandler); // <- What if somebody defines this handler and you overriding it - you should check this at least...
One question : there is the 'Crash' package in Uppsrc, I don't know how to use it but I suspect it could be of great interest...
Quote:Ah, that explains it. So you have like 2 weeks of experience with this issueAfter thirty five years programming, I think it's better late than never.
If I'd be an expert in this issue, I wouldn't ask for advice.
koldo wrote on Tue, 27 October 2020 11:36Quote:Ah, that explains it. So you have like 2 weeks of experience with this issueAfter thirty five years programming, I think it's better late than never.
If I'd be an expert in this issue, I wouldn't ask for advice.
Well, you did not seem to ask for an advice. You actually seemed to force an answer on us
Mirek
f your code considers these situations as normal, my friend, you are screwed
Hello Koldo,
Quote:f your code considers these situations as normal, my friend, you are screwed
I don't know what made you think that was my point of view, and it is not at all my vision (rather the contrary), the only thing I say is that code comes from everywhere and quality is not always as it should be.
So everything that can be done to detect and correct this must be done and made available (including detecting divisions by 0, overflows, ...)
And besides, no one is perfect, errors are everywhere all the time (especially with big teams): so you have to deal with them
The calculation of your bank account balance is infinite
Anyone have an opinion on this one? I'm of the opinion that floats should not be used to represent any kind of currency or bank balance and I tended to replace them in C# with Decimal when I worked for an accounting department. I haven't checked but does U++ have a fixed-point decimal type for dealing with currency and other numbers where float accuracy is not good enough? If not maybe one should be made.