Handling exceptions
In Java, when things go wrong, they can be classified as errors or exceptions. An error is a problem that cannot be recovered from. An exception is an error that can be detected in your code such that you can possibly recover from it. For example, a recursion that never ends will result in a StackOverflowError
-type error. Converting the Bob
string to an integer will result in a NumberFormatException
exception.
Here is a diagram of the primary exception classes:
Figure 7.1 – The exception hierarchy
Exceptions are objects of classes named after the type of exception that has occurred. In the diagram, you can see that at the root of the hierarchy is the Throwable
class. From Throwable
, we have two subclasses: Error
and Exception
. The subclasses of Error
are named after the errors that may occur during program execution. These are errors that generally cannot be recovered from and should lead to the program ending.
As it may...