You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Develop code that handles multiple Exception types in a single catch block.
9
-
-
10
-
Desenvolver código que lide com múltiplos tipos de Exception em um único bloco catch.
11
9
----
12
10
13
-
É esperado que o candidato saiba compreender e analisar o uso da instrução _try-catch_ com múltiplos tipos de `Exception` no mesmo bloco `catch`.
11
+
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.
14
12
15
-
Antes de continuar, com base no exemplo a seguir, entenda a execução do método `main` e o que é apresentado no console após sua execução.
13
+
Before proceeding, based on the following example, understand the execution of the `main` method and what is presented on the console after its execution.
O código anterior possui um bloco _try-catch_ que você provavelmente já conhece. A novidade neste código está no primeiro bloco `catch`, onde várias exceções são declaradas e capturadas ao mesmo tempo.
21
+
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.
24
22
25
-
.Saída no console
23
+
.console output
26
24
[source,console]
27
25
----
28
-
Exceção capturada: java.lang.NullPointerException
26
+
Exception caught: java.lang.NullPointerException
29
27
----
30
28
31
-
. Desde o Java 7, múltiplas exceções podem ser capturadas no mesmo `catch`.
29
+
. Since Java 7, multiple exceptions can be caught in the same catch.
32
30
33
-
. Apenas uma variável é permitida para um bloco `catch`, e deve estar localizada no final.
31
+
. Only one variable is allowed for a `catch` block, and must be located at the end.
. Ao fazer `catch` de múltiplas `Exception`, não é permitido sobrescrever a variável da exceção. Mas é possível se for apenas uma `Exception` no `catch`.
48
+
. 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`.
* Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 291). Wiley. Edição do Kindle.
84
+
* Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 291). Wiley. Kindle Edition.
87
85
88
86
* https://docs.oracle.com/javase/8/docs/technotes/guides/language/catch-multiple.html[Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking.] Java Documentation.
Copy file name to clipboardExpand all lines: src/org/j6toj8/languageenhancements/multipleexception/MultipleException_GenericsLower.java
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,9 @@ public static void main(String[] args) {
8
8
try {
9
9
thrownewNullPointerException();
10
10
} catch (Exceptione) {
11
-
System.out.println("Exceção capturada: " + e);
12
-
} catch (NullPointerException | IllegalArgumentExceptione) { // NÃO COMPILA - NullPointerException é mais específico que Exception, logo deveria ser capturada antes de Exception
13
-
System.out.println("Exceção capturada: " + e);
11
+
System.out.println("Exception caught: " + e);
12
+
} catch (NullPointerException | IllegalArgumentExceptione) { // NOT COMPILING - NullPointerException is more specific than Exception, so it should be caught before Exception
Copy file name to clipboardExpand all lines: src/org/j6toj8/languageenhancements/multipleexception/MultipleException_Redundant.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,8 @@ public static void main(String[] args) {
9
9
10
10
try {
11
11
thrownewNullPointerException();
12
-
} catch (RuntimeException | IllegalArgumentExceptione) { // NÃO COMPILA - IllegalArgumentException herda de RuntimeException, logo seria redundante
13
-
System.out.println("Exceção capturada: " + e);
12
+
} catch (RuntimeException | IllegalArgumentExceptione) { // NOT COMPILING - IllegalArgumentException inherits from RuntimeException, so would be redundant
0 commit comments