3 ms·
I strongly disagree that exceptions are about catching unforeseen issues at a lower level. Exceptions should almost never be caught. A well-written program will
by barrkel 17y ago
I strongly disagree that exceptions are about catching unforeseen issues at a lower level. Exceptions should almost never be caught. A well-written program will have only a handful of exception handlers that don't repropagate the exception; ideally only one, at the highest level of the event loop or request handler.
As to your string decoder example, the sensible default should be to throw an exception, with an easily available out to replacements etc. as you suggest. String conversion across multiple code pages is an interesting example precisely because data loss is so easy, and if the problems aren't raised early in the data flow, it can be hard to figure out where it went wrong. The "default" manifests itself in Windows programs as '?' - rather annoying to debug when the first time you see it is on the UI, while other concrete are plentiful on the web. A very common example is balanced "smart" quotes, which frequently end up encoded in either the web page or the RSS feed in a different encoding than that described in the HTTP header, or in the meta tag, or according to the relevant standards, etc.
Yes, runtime errors should be avoided, but not by the lower-level code; rather, the lower-level code should throw exceptions when error conditions occur, but have ways and means for programs that care about the exceptional conditions to not throw exceptions. This isn't always possible, though. Consider TFA's example of file not found:
> The caller can't recover from a file not found error
The caller can test to see if a file exists before trying to read it, but non-transactional interactions like this with the file system are subject to races; the file might exist one microsecond and be gone the next. But it is also obviously true that the caller can recover from a file not found error. Concrete example: consider a thumbnail generating service, which checks to see if a cached thumbnail file exists before doing the expensive work of creating it if necessary. Here, a file not found exception (perhaps caused by concurrent cleanup of cache, for random operational reasons) is indeed not fatal.
- jacquesm 17y ago> Exceptions should almost never be caught. That's exactly where the controversy lies. The string conversion issue was brought up because I think it is an excellent case of 'programmer knows best', but the exception causes the whole dataset to be rejected, not just the one record out of 3.9 million that were fine. That's obviously sub-optimal. There has to be a better way to deal with that sort of thing, such as for instance an 'output' vector where data that couldn't be processed can be sent, or a series of warnings / errors attached to certain offsetts in your data. Then after running the conversion you can decide if you want to drop all of it (which is what happens now) and fix the input data, or discard those records (sometimes that is an option), or manually edit those and then run the import on those few records again. Because exceptions are a mechanism you can then 'use' them for whatever you feel like. Just like you can use setjmp/longjmp in C for all kinds of creative stuff you can use exceptions to change the dynamic of your program. And this leads to some very difficult to trace code because exceptions break the normal flow. Almost all exceptions in for instance the python mysql layer need to be handled by your application otherwise you will be exposing the user of your application (or website) to a runtime error and that is not good, not handling those is not really an option (unless you don't like your users much :) ). So, in production grade software you'll find that almost all possible exceptions are indeed handled, and if possible as close as possible to the location of the error to make sure the fall-out of the problem is minimized. Generally speaking, the higher up the handler the less information there is about the context of the exception, the smaller the chance you can handle it in a meaningful way. This style of coding makes it not rare to see half the code to be about exception handling, the other half actual 'productive' code. Especially Java suffers from this very much.
- barrkel 17y agoFor your string example, a callback would probably be the most flexible solution, but I still believe that not signalling corruption when it is found by library code is irresponsible. Batch processing code will have a loop; that's precisely where there should be an exception handler, one that can log that there were errors with this particular record, so that attention can be brought in later. If the data isn't too important, then sure, minor corruption can be ignored, but it would be a terrible default. Taking your database interaction example, here are the kinds of situations I could see, and responses: * Failure to connect; this is pretty fatal, unless there's home-spun failover support, it should not be handled at a low level * Query syntax / API error: this is a programming error, should be logged and the current process (request, whatever) aborted * Constraint failure: choosing whether to handle constraints on the database side, or on the app server side, or both sides, is an architectural decision with tradeoffs. Ideally constraints should be portable, so that they can be transported all the way to the UI, and checked at every stage along the way. These constraint failures might be signalled through exceptions, but that's a tactical choice, and may not be the best one. Generally, exceptions only signal for the first error, but usually there is more than one constraint, and multiple actionable failed constraints should be propagated to avoid wasting the end user's time (consider the simplistic compiler which halts on first error). My point is that exceptions should generally either be things you can't reasonably handle, with a non-throwing "out" for situations where you do expect to be able to handle the problem. If you expect to be able to work around the issue, i.e. find a solution locally, low down in the stack, then you probably shouldn't be catching an exception in order to do so, unless that's impossible (like the race condition I mentioned earlier).
- jacquesm 17y ago> My point is that exceptions should generally either be things you can't reasonably handle That we completely agree on. As for that race condition, I think that a situation like that could easily be avoided by having a single task be responsible for maintaining that data structure and to send it messages to update the state atomically. That way you avoid the race completely. Race conditions usually are a sign of multiple parties having access to a single resource where you'd be better served having one 'handler' process in charge of the data object that multiple parties want to access. It can then be the arbiter of who has access. If the changes are a-synchronous then you could use a 'fire and forget' style queuing mechanism where the only thing that matters is that you know the message was received, the more tightly coupled example you gave could be handled reasonably elegant with a client-server model where the server will handle only client at a time for a particular resource. Data objects not being 'alive' enough is the cause of many race conditions, by assigning a handler you achieve two things in one go, the first is an abstraction of interaction with the data object to a series of messages to a handler, the second the removal of race conditions and their 'normal' solutions (such as locks).