3 ms·
Heh, I thought the answer was "Because my blog is named uberpython and it would be way confusing." :-) But error handling is one of those topics that really ge
by ChuckMcM 14y ago
Heh, I thought the answer was "Because my blog is named uberpython and it would be way confusing." :-)
But error handling is one of those topics that really gets people going. From ABEND in the old IBM batch days, to uncatchable signals like SEGV in unix and uncaught exceptions in C++ or Java.
I tend to come down on the "decide what you are going to do in the code, right where the error occurs" flavor of the argument. So checking for errors when they occur is important to me, what to do next gets to be sticky.
So there are three things you want to do:
1) Easy - you just want to quit/die/exit pretend you never existed. This is pretty easy and most OS'es and embedded toolkits have something along the lines of 'exit' or 'exit_with_dump.' So that later you can try to reconstruct what happened.
2) Is "this is bad, but it might be ok later" where you want to unwind to the point where this started but not completely exit. Exceptions are pretty good for this if used well since you can catch them at the 'unwind' point and if you can attach the unwinding actions (closing files, freeing memory, etc) to the return stack then it can be managable.
3) You want to plod along in a degraded mode, which means you need some way of communicating with the rest of your code that you're damaged goods at some level.
Go has an interesting mix which I haven't used extensively but I wouldn't dismiss it out of hand. The folks writing it run services that continue through partial outages so if it were too egregious folks would rebel inside of Google.
- sitharus 14y agoThe problem I found is #1 is hard. In C# if I want to just exit/return an HTTP error I ignore the exception and it happens. In Go for the majority of cases I have to check the error and call a panic function. I wrote a helper function to do this - it takes one parameter and panics if not nil. 2 and 3 I've found to be at little less verbose as there's no try {...} catch{...} around it. It seems a nice way of avoiding the stack sentinels or setjmp/longjmp that C++ has, which means the output is less architecture dependent. Personally I don't think Go is quite ready for prime time, but it's definitely promising. I'm doing a couple of projects with it to see how it runs.
- zmj 14y agoHave you seen the Error function in the HTTP package? http://golang.org/pkg/net/http/#Error http://golang.org/pkg/net/http/#Error
- dwc 14y agoYou are right that #2 is well served by exceptions when done right. It's also well served by checking locally and returning your own error status. Both ways can be done well. IME exceptions tend to pass the buck to the caller when they shouldn't, in the hands of mediocre programmers.
- forgotusername 14y agoJust some more oil for the fire. Having worked primarily in Python for over 10 years (and generally been a huge enthusiast during that time), I'm pretty comfortable at this stage with the notion that exception handling languages contribute significantly in only one respect - they allow newbies to write code without ever properly understanding how errors should be managed without violating layering, where retries should be inserted, and so on. The culture of writing code, and that code being fine once a unit test is passing seems to propagate the myth that it's fine for any code to throw any exception at any time – it doesn't matter as long as those errors expected to occur from testing are the only conditions that ever occur. As for the rest, well. Kaboom! Starting off from C, it doesn't take you more than your first 100 lines before you discover pain due to missing a return value. In my experience, eventually 60% of your time is spent wondering how this particular return value propagates throughout the rest of the code you've written, and libraries in use. The end result is my C code tends to be much more robust in the face of braindamage (say, network errors, IO errors) than my Python, simply as a result of a cultural mindset that basically encourages ignorance of error conditions. You can also trace one of the biggest pains from Python 2 back to this culture: deferred error handling is at the very core of the Unicode/strings mess, probably the single most motivating factor for moving to Python 3. [Extremely tired, aware this is poorly written and sways between points, but hope it makes some sense]
- zvrba 14y ago> to uncatchable signals like SEGV in unix and uncaught exceptions in C++ or Java. SEGV can be caught. There are only 3 uncatchable signals: KILL, STOP and CONT.