logoalt Hacker News

rorylaitilayesterday at 6:23 PM5 repliesview on HN

Exceptions get a lot of hate, but of the three styles, I keep coming back to exceptions. Ages ago I built an application with error codes, and went back to exceptions, because I thought the ceremony of error checking was not worth it. On occasion, I'll use a get-last-error style, particularly when the error is something the user is intended to address. But for most of my applications (which are usually not libraries and are code under my control) I like exceptions.

I always have global error handler that logs and alerts of anything uncaught. This allows me to code the happy path. Most of the time, it's not worth figuring out how to continuing processing under every possible error, so to fail and bail is my default approach. If I later determine that its something that can be handled to continue processing, then I update that code path to handle that case.

Most of my code is web applications, so that is where I'm coming from.


Replies

vips7Ltoday at 3:57 AM

I love exceptions. I also think they’re the best mechanism of error handling, but in my experience a lot of developers just don’t seem to “get” exceptions. They seem to be scared of them from runtime crashes or they try to predict when they’ll happen instead of just catching/responding. I really think that’s because most implementations of exceptions are not checked.

I really think that a language that invested in them properly more developers would come to see their value. I truly hope that will be Java some day. Making checked exceptions work across lambdas and providing language constructs to uncheck them would go miles.

PaulHouleyesterday at 7:28 PM

Hell yeah. I typed in my first C program, a terminal emulator, from a 1984 issue of Byte magazine. It was painful seeing how 5 lines of real logic were intertwined with 45 lines of error handling logic that, in the end, did what exceptions did for free -- it was a formative experience for me as a programmer and when I saw exceptions in Java in 1995 (still in beta) they made me so happy.

In the async case you can pass the Exception as an object as opposed to throwing it but you're still left with the issue that the failure of one "task" in an asynchronous program can cause the failure of a supertask which is comprised of other tasks and handling that involves some thinking. That's chess whereas the stuff talked about in that article is Tic-Tac-Toe in comparison.

show 1 reply
bob1029yesterday at 7:43 PM

Exceptions can even work with remote APIs.

If you reach into the enterprise bucket of tricks, technologies like WCF/SOAP can propagate these across systems reliably. You can even forward the remote stack traces by turning on some scary flags in your app.config. When printing the final exception using .ToString(), this creates a really magical narrative of what the fuck happened.

The entire reason exceptions are good is because of stack traces. It is amazing to me how many developers do not understand that having a stack trace at the exact instant of a bad thing is like having undetectable wall hacks in a competitive CS:GO match.

show 3 replies
PaulKeebleyesterday at 8:29 PM

Agreed and even more heretical from me is that I quite like declared exceptions. It makes the interface of a method clear in all the ways it can fail and you can directly choose what to handle often without having to look at the docs to work out what they mean, because the names tell you what you need to know. You can ignore them and rethrow catch globally but you can also handle them.

Having used Go for years now frankly I prefer exceptions, way too often there is nothing that can be done about an error locally but it produces noise and if branches all over the code base and its even worse to add an error later to a method than in Java because every method has to have code added not just a signature change. I really miss stack traces and the current state of the art in Go has us writing code to produce them in every method.

show 3 replies
o11cyesterday at 8:47 PM

The problem with `getlasterror` and `errno` is that they're global (thread-local, whatever).

But if you make them take a `context` object, there's no longer a problem.

One interesting observation - you can use them even for the initial "failed to allocate a context" by interpreting a NULL pointer as always containing an "out of memory" error.