Error handling

Error handling is a very interesting topic in itself. Although it does receive attention, I think it still isn't getting as much as it deserves. It's my personal opinion that correctly handling when things go wrong is just as important as correctly implementing when things go right, at least if your goal is to write a well-behaved library or application. Rust uses the best method of error handling I've come across so far, and I've seen some languages also pick that method up over the years.

When I'm speaking of error handling in this chapter, I always mean handling of recoverable errors. Handling unrecoverable errors should be a solved problem at this point: Unwind the stack, drop everything that goes out of scope in the correct order. Beyond that, it's a matter of deallocating system resources while stack unwinding. That means it will release locks, file handlers, network sockets and so on.

To start off this chapter, let's take a look at different error handling methods over the years.