3 Exceptions vs other patterns of handling errors in your code
This chapter covers
- Best patterns for handling exceptions
- Exceptions from third-party libraries
- Exceptions in multi-threaded and async code
- Exceptions in functional and object-oriented programs
Errors and exceptions are inevitable in our code. Almost every code path fails if something unexpected happens. Imagine that you are executing a simple addition of two numbers. At first glance, such a code path cannot fail. However, you need to be aware that your program executes in some context. For example, you may get out of memory error if there is not enough memory to run any operation on your machine. You may get an interrupted exception in the multi-threaded context if your code is executed in a separate thread, and this thread gets the interruption signal. There are a lot of potential problems that can occur.
Most often, our code is not trivial, and it can fail in a variety of ways. Handling failures should be our first thought when creating our code. Our code should be fault-tolerant, meaning that it should recover from problems whenever possible. Before you can decide how to handle exceptions, you need to design an API that tackles problems and signals them in an explicit way. However, if we signal the possibility of every error explicitly, our code would become hard to read and maintain.