Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
What's the best way to stop at an error in a program?
kitfox last edited by
I come from a Java background and am used to throwing an exception when I check something and find it has an invalid value. I tend to use them much like assert statements - I'm just checking to make sure the program has not entered an invalid state, and if it has I want the program to halt at that point and print an error message. I rarely catch thrown exceptions since usually they represent faulty behavior and there is little point in continuing.
What would be the best way to do something similar in Qt/C++? I've tried adding in throw clauses, but they cause my program to exit without printing an error message:
if (index < 0) throw "Index should be greater than 0";
If you use expections you will also need to catch it at some point. Otherweise your program exits for an uncaught expection.
I am not a big fan of exceptions. You should rather design your program to handle the invalid state properly by your software design.
elfring last edited by
I've tried adding in throw clauses, but they cause my program to exit without printing an error message:
I suggest to take another look into corresponding information sources like the article “Quality Matters #6: Exceptions for Practically-Unrecoverable Conditions”.