5 ms·
For 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
by barrkel 17y ago
For 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).
- barrkel 17y agoThe race condition example applied to a pretty global "single resource": the file system; and came from the example in the main article. In situations where you have greater control over the resource, I agree that serialization / independence of operations, whether by transactions, queuing, single arbiter, etc., is better.