Timeline for When to use assertions and when to use exceptions?
Current License: CC BY-SA 2.5
8 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 19 at 17:08 | comment | added | user281377 | @HartmutBraun Several sanity checks in a single try-catch-block makes it unnecessarily difficult to find out which check actually failed. I would rather avoid that. | |
| Jul 18 at 15:51 | comment | added | Hartmut Braun | @user281377 assume a subroutine that starts with several sanity checks until it finally can start the actual job. There’s two outcomes: all passed or any failed. Putting all sanity checks in a try-catch-block is essentially several goto‘s to the exception handler. Ugly? You can do the same with several return statements. Has its own drawbacks. You can carry an success Boolean across the sanity checks. Has its own drawbacks. Using an exception to deliberately ‚goto‘ the caller is a totally different beast. | |
| Jul 17 at 14:19 | comment | added | user281377 | @Hartmut Braun I truely hope you do not mean using exceptions as a replacement for goto, which is possible but obviously so ugly that it should not be a controversially discussed topic. | |
| Jul 17 at 14:19 | comment | added | user281377 | @gnasher729 In some cases, it's easier to parse the user supplied input in a try-catch- block rather than verifiying it independenty. | |
| Jan 1, 2020 at 16:18 | comment | added | Hartmut Braun | If it is a good idea to use exceptions for normal control flow is a very controversially discussed topic. | |
| Dec 31, 2019 at 16:43 | comment | added | gnasher729 | If the user inputs something incorrect, that should be expected, not an exception. | |
| Oct 22, 2018 at 21:38 | comment | added | Mateen Ulhaq | This is a pretty good way to put it. If the user inputs something incorrect, throw an exception. If the input is correct but something's still wrong, throw an assert. | |
| Oct 29, 2010 at 9:04 | history | answered | user281377 | CC BY-SA 2.5 |