4 ms·
The problem with the above code is without exceptions you know where the program failed 23843 or 28783 but with exceptions all you know is something is wrong.
by Retric 18y ago
The problem with the above code is without exceptions you know where the program failed 23843 or 28783 but with exceptions all you know is something is wrong.
- olavk 18y agoYou can add error codes and whatever else you want to propagate to an exception object.
- Retric 18y agoTrue in theory but the code given: catch database error { warn "Whoa, your database is broken!"; } Ignores all that and hides the error. I only point this out because I have seen a lot of real world code where people skip even the assert in the catch statement. Edit: You can add all of this stuff manually (or as part of the warn function) but I would like to see a language where you can define a standard place where all errors are logged independent of the given code unless it’s explicitly overridden. So you people can't add empty catch blocks.
- jrockway 18y agoIn Perl, you can set $SIG{__DIE__} to a function that gets all exceptions. You can also override CORE::die or CORE::GLOBAL::die.
- aggieben 18y agobzzt. depending on the language and the author of the exception-throwing code, the exception will contain information about where the error occurred and what might have caused it.
- Retric 18y agoTrue, but in the above code such information is not propagated outside of the function. If you want to add that you need to throw another exception inside of the catch block or skip the catch.
- natrius 18y agoWhich is exactly the right thing to do. I don't see the problem.
- Retric 18y agoAt this point, I'm tired of typing the code. Checking for errors after every function call is way too much work Think of what the exception code ends up as in ASM. Basically try transparently wraps system calls with the same type of error handling code as his example. However, if you want to write stable code you need to account for each of these errors anyway. So when your DB connection fails you need to know if it’s a TCP/IP error or a Bad Password etc which means either wrapping each line with its own try catch or decoding the exception within a catch block. Granted, exception handling let’s your write code for the ideal case and avoid crashing when something messes up but as soon as you want to recover from an error without starting over you end up writing the same code anyway. And because the happy path works it’s not obvious just how far you are from high quality code.
- aggieben 18y agoSee my later comment about expected errors versus unexpected ones. Exceptions were never meant to replace good error-handling code.