Summary
We have taken a very hands-on approach with this chapter. We started by making your code break in different ways, and then explained the differences between an error and an exception. We focused on ways to handle the latter because those are the only ones that should not make your program crash immediately.
Exceptions can be handled by catching or throwing. The former is done by observing the different exceptions and defining different strategies to respond to the situations by means of a try-catch statement. You have the option of either resending the exception to a different class with the throw
or responding within the catch
block. Independently of what strategy you follow, you can set the system to execute some final lines of code after handling the exception using the finally
block.
This chapter also included a series of recommendations on how to deal with exceptions on a more conceptual level. You have a list of best practices that any professional programmer...