Java ErrorsLesson 47Free preview
Java Multiple Exceptions
This lesson is part of Java · All levels
Summary
One try block may be accompanied by several catch blocks to handle different types of exceptions. Since Java 7, we can also capture several exceptions at once in a block with a separator. The key is to write catch blocks from the most specific to the most common.
Key points
- One try can have many catch, each for a different type of exception.
- Multi-catch: catch (IOException) SQLException e catches two types in one block
- The catch sequence must be from a (specific) derivative class to a (common) parent class, otherwise the compiler rejects
- The variable exception on multi-catch is final so it cannot be reassigned
- The block catch exception at the end becomes a safety net for unexpected error
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.