Skip to content

Latest commit

 

History

History
88 lines (68 loc) · 3.27 KB

03-multiple-exception.asc

File metadata and controls

88 lines (68 loc) · 3.27 KB

Handles multiple Exception in a single catch

Objective
Develop code that handles multiple Exception types in a single catch block.

The candidate is expected to understand and analyze the use of the try-catch statement with multiple types of Exception in the same catch block.

Before proceeding, based on the following example, understand the execution of the main method and what is presented on the console after its execution.

src/org/j6toj8/languageenhancements/multipleexception/MultipleException_Complete.java
link:../../../src/org/j6toj8/languageenhancements/multipleexception/MultipleException_Complete.java[role=include]

The previous code has a try-catch block that you probably already know. The new about this code is in the first catch block, where multiple exceptions are thrown and caught at the same time.

console output
Exception caught: java.lang.NullPointerException
  1. Since Java 7, multiple exceptions can be caught in the same catch.

  2. Only one variable is allowed for a catch block, and must be located at the end.

    src/org/j6toj8/languageenhancements/multipleexception/MultipleException_MultipleSameCatch.java
    link:../../../src/org/j6toj8/languageenhancements/multipleexception/MultipleException_MultipleSameCatch.java[role=include]
  3. It is not allowed to declare different exceptions, but would be redundant considering inheritance.

    src/org/j6toj8/languageenhancements/multipleexception/MultipleException_Redundant.java
    link:../../../src/org/j6toj8/languageenhancements/multipleexception/MultipleException_Redundant.java[role=include]
  4. When catching multiple Exceptions, it is not allowed to override the exception variable. But it’s possible if it’s just an Exception in catch.

    src/org/j6toj8/languageenhancements/multipleexception/MultipleException_OverrideVar.java
    link:../../../src/org/j6toj8/languageenhancements/multipleexception/MultipleException_OverrideVar.java[role=include]
  5. As in previous releases, more generic types of Exception should come later, lower in the catch.

    src/org/j6toj8/languageenhancements/multipleexception/MultipleException_GenericsLower.java
    link:../../../src/org/j6toj8/languageenhancements/multipleexception/MultipleException_GenericsLower.java[role=include]
  6. As in previous versions, repeated exceptions are still prohibited.

    src/org/j6toj8/languageenhancements/multipleexception/MultipleException_RepeatException.java
    link:../../../src/org/j6toj8/languageenhancements/multipleexception/MultipleException_RepeatException.java[role=include]
  7. As in previous versions, Checked Exceptions (those that inherit from Exception) can only be in a catch if something in try throws them.

    src/org/j6toj8/languageenhancements/multipleexception/MultipleException_CheckedException.java
    link:../../../src/org/j6toj8/languageenhancements/multipleexception/MultipleException_CheckedException.java[role=include]
References