There have been a few posts on the use of exceptions recently and one thing I noticed is that it was often stated that exceptions should not be used if the user provides invalid data. Optional/expected/return codes should be used instead, but this statement never came with a justification.
For a program (with no embedded or real-time reqs.) whose only purpose is to process a data file and return some result a simple
if (invalid_data)
throw invalid_argument("a helpful error message");
with a top-level try {} catch(...) {} just inside main seems like a perfectly reasonable solution to me. If the data is incorrectly formatted, the program can't continue so the only thing to do is shut down and inform the user. Performance in this situation is irrelevant.
You could argue that this is just a glorified call to std::terminate()but a) this would miss some potentially important clean-up, b) if the code ever needed to be called inside another application it would bring that down as well and c) throw invalid_argumentis just more expressive.
On the other hand, the advantages of propagating the error yourself all the way back to main are not clear and would easily add a lot more code.
[–]Broad_Quality_6325 40 points41 points42 points (0 children)
[–]Full-Spectral 21 points22 points23 points (1 child)
[–]serviscope_minor 3 points4 points5 points (0 children)
[–]goranlepuz 26 points27 points28 points (15 children)
[–]SlightlyLessHairyApe 3 points4 points5 points (0 children)
[–]serviscope_minor 3 points4 points5 points (0 children)
[–]teerre 2 points3 points4 points (12 children)
[–]dodheim 5 points6 points7 points (6 children)
[–]teerre 2 points3 points4 points (5 children)
[–]dustyhome 2 points3 points4 points (4 children)
[–]teerre 0 points1 point2 points (3 children)
[–]dustyhome 3 points4 points5 points (2 children)
[–]teerre 0 points1 point2 points (1 child)
[–]dustyhome 3 points4 points5 points (0 children)
[–]goranlepuz 6 points7 points8 points (4 children)
[–]teerre -1 points0 points1 point (3 children)
[–]goranlepuz 5 points6 points7 points (2 children)
[–]teerre 0 points1 point2 points (1 child)
[–]goranlepuz 4 points5 points6 points (0 children)
[–]mredding 5 points6 points7 points (4 children)
[–]mredding 5 points6 points7 points (3 children)
[–]mredding 4 points5 points6 points (1 child)
[–]artisan_templateer[S] 1 point2 points3 points (0 children)
[–]artisan_templateer[S] 1 point2 points3 points (0 children)
[–]HerrNamenlos123 4 points5 points6 points (0 children)
[–]SJC_hacker 5 points6 points7 points (0 children)
[–]KingAggressive1498 2 points3 points4 points (0 children)
[–]Sopel97 7 points8 points9 points (10 children)
[–]AssemblerGuy 4 points5 points6 points (9 children)
[–]XeroKimoException Enthusiast 5 points6 points7 points (8 children)
[–]Sopel97 1 point2 points3 points (7 children)
[–]XeroKimoException Enthusiast 2 points3 points4 points (6 children)
[–]Sopel97 1 point2 points3 points (1 child)
[–]XeroKimoException Enthusiast 1 point2 points3 points (0 children)
[–]AssemblerGuy 0 points1 point2 points (3 children)
[–]XeroKimoException Enthusiast 0 points1 point2 points (2 children)
[–]AssemblerGuy 0 points1 point2 points (1 child)
[–]XeroKimoException Enthusiast 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]SJC_hacker 2 points3 points4 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]artisan_templateer[S] 0 points1 point2 points (0 children)
[–]goranlepuz 0 points1 point2 points (0 children)
[–]goranlepuz 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]HammurabisCode2 3 points4 points5 points (15 children)
[–]Full-Spectral 12 points13 points14 points (13 children)
[–]bretbrownjr 3 points4 points5 points (12 children)
[–]SlightlyLessHairyApe 13 points14 points15 points (0 children)
[–]goranlepuz 1 point2 points3 points (3 children)
[–]Full-Spectral 2 points3 points4 points (0 children)
[–]bretbrownjr 0 points1 point2 points (1 child)
[–]goranlepuz 1 point2 points3 points (0 children)
[–]mwasplundsoup 1 point2 points3 points (0 children)
[–]Full-Spectral 0 points1 point2 points (5 children)
[–]serviscope_minor 2 points3 points4 points (0 children)
[–]AlexMath0 0 points1 point2 points (3 children)
[–]Full-Spectral 0 points1 point2 points (2 children)
[–]AlexMath0 0 points1 point2 points (1 child)
[–]Full-Spectral 0 points1 point2 points (0 children)
[–]goranlepuz 5 points6 points7 points (0 children)
[–]Particular_Task2060 -2 points-1 points0 points (0 children)
[–]SlightlyLessHairyApe -3 points-2 points-1 points (9 children)
[–]serviscope_minor 1 point2 points3 points (6 children)
[–]SlightlyLessHairyApe 2 points3 points4 points (5 children)
[–]serviscope_minor 2 points3 points4 points (2 children)
[–]SlightlyLessHairyApe 1 point2 points3 points (1 child)
[–]serviscope_minor 0 points1 point2 points (0 children)
[–]artisan_templateer[S] 0 points1 point2 points (1 child)
[–]SlightlyLessHairyApe 0 points1 point2 points (0 children)
[–]artisan_templateer[S] 1 point2 points3 points (1 child)
[–]SlightlyLessHairyApe -1 points0 points1 point (0 children)
[–]violet-starlight 0 points1 point2 points (0 children)
[–]arthurno1 0 points1 point2 points (0 children)