Skip to content

Commit 81639b9

Browse files
committed
feat(lang-enh): 🎸 handles multiple Exception, translated
Refers: #4
1 parent f76ca26 commit 81639b9

File tree

8 files changed

+36
-38
lines changed

8 files changed

+36
-38
lines changed

book/02-language-enhancement/sections/03-multiple-exception.asc

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
11
:java-package: src/org/j6toj8/languageenhancements
22
:section-java-package: ../../../{java-package}
33

4-
=== Múltiplas `Exception` no mesmo `catch`
4+
=== Handles multiple Exception in a single catch
55

6-
.Objetivo
6+
.Objective
77
----
88
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.
119
----
1210

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.
1412

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.
1614

1715
[source,java,indent=0]
1816
.{java-package}/multipleexception/MultipleException_Complete.java
1917
----
2018
include::{section-java-package}/multipleexception/MultipleException_Complete.java[tag=code]
2119
----
2220

23-
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.
2422

25-
.Saída no console
23+
.console output
2624
[source,console]
2725
----
28-
Exceção capturada: java.lang.NullPointerException
26+
Exception caught: java.lang.NullPointerException
2927
----
3028

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.
3230

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.
3432
+
3533
[source,java,indent=0]
3634
.{java-package}/multipleexception/MultipleException_MultipleSameCatch.java
3735
----
3836
include::{section-java-package}/multipleexception/MultipleException_MultipleSameCatch.java[tag=code]
3937
----
4038

41-
. Não é permitido declarar exceções diferentes, mas que seriam redundantes levando em consideração a herança.
39+
. It is not allowed to declare different exceptions, but would be redundant considering inheritance.
4240

4341
+
4442
[source,java,indent=0]
@@ -47,15 +45,15 @@ include::{section-java-package}/multipleexception/MultipleException_MultipleSame
4745
include::{section-java-package}/multipleexception/MultipleException_Redundant.java[tag=code]
4846
----
4947

50-
. 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`.
5149
+
5250
[source,java,indent=0]
5351
.{java-package}/multipleexception/MultipleException_OverrideVar.java
5452
----
5553
include::{section-java-package}/multipleexception/MultipleException_OverrideVar.java[tag=code]
5654
----
5755

58-
. Assim como nas versões anteriores, tipos mais genéricos de `Exception` devem vir depois, mais abaixo nos _catch's_.
56+
. As in previous releases, more generic types of `Exception` should come later, lower in the _catch_.
5957

6058
+
6159
[source,java,indent=0]
@@ -64,26 +62,26 @@ include::{section-java-package}/multipleexception/MultipleException_OverrideVar.
6462
include::{section-java-package}/multipleexception/MultipleException_GenericsLower.java[tag=code]
6563
----
6664

67-
. Assim como nas versões anteriores, Exceções repetidas ainda são proibidas.
65+
. As in previous versions, repeated exceptions are still prohibited.
6866
+
6967
[source,java,indent=0]
7068
.{java-package}/multipleexception/MultipleException_RepeatException.java
7169
----
7270
include::{section-java-package}/multipleexception/MultipleException_RepeatException.java[tag=code]
7371
----
7472

75-
. Assim como nas versões anterior, Exceções checadas (aquelas que herdam de `Exception`) só podem estar em um `catch` caso algo no `try` lance elas.
73+
. As in previous versions, Checked Exceptions (those that inherit from `Exception`) can only be in a `catch` if something in `try` throws them.
7674
+
7775
[source,java,indent=0]
7876
.{java-package}/multipleexception/MultipleException_CheckedException.java
7977
----
8078
include::{section-java-package}/multipleexception/MultipleException_CheckedException.java[tag=code]
8179
----
8280

83-
.Referências
81+
.References
8482
****
8583
86-
* 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.
8785
8886
* 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.
8987

src/org/j6toj8/languageenhancements/multipleexception/MultipleException_CheckedException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public static void main(String[] args) {
99

1010
try {
1111
throw new NullPointerException();
12-
} catch (NullPointerException | IOException e) { // NÃO COMPILA - IOException não é lançada dentro do bloco try
13-
System.out.println("Exceção capturada: " + e);
12+
} catch (NullPointerException | IOException e) { // NOT COMPILING - IOException is not thrown inside try block
13+
System.out.println("Exception caught: " + e);
1414
}
1515
}
1616
// end::code[]

src/org/j6toj8/languageenhancements/multipleexception/MultipleException_Complete.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public static void main(String[] args) {
88
try {
99
throw new NullPointerException();
1010
} catch (NullPointerException | IllegalArgumentException | IllegalStateException e) {
11-
System.out.println("Exceção capturada: " + e);
11+
System.out.println("Exception caught: " + e);
1212
} catch (Exception e) {
13-
System.out.println("Exceção capturada: " + e);
13+
System.out.println("Exception caught: " + e);
1414
}
1515
}
1616
// end::code[]

src/org/j6toj8/languageenhancements/multipleexception/MultipleException_GenericsLower.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public static void main(String[] args) {
88
try {
99
throw new NullPointerException();
1010
} catch (Exception e) {
11-
System.out.println("Exceção capturada: " + e);
12-
} catch (NullPointerException | IllegalArgumentException e) { // 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 | IllegalArgumentException e) { // NOT COMPILING - NullPointerException is more specific than Exception, so it should be caught before Exception
13+
System.out.println("Exception caught: " + e);
1414
}
1515
}
1616
// end::code[]

src/org/j6toj8/languageenhancements/multipleexception/MultipleException_MultipleSameCatch.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public static void main(String[] args) {
99

1010
try {
1111
throw new NullPointerException();
12-
} catch (NullPointerException | IllegalArgumentException e) { // COMPILA - múltiplas exceções no mesmo catch, só uma variável no final
13-
System.out.println("Exceção capturada: " + e);
14-
} catch (IllegalStateException ise | UnsupportedOperationException uoe) { // NÃO COMPILA - mais de uma variável declarada
15-
System.out.println("Exceção capturada: " + ise);
16-
} catch (ClassCastException cce | ConcurrentModificationException) { // NÃO COMPILA - só uma variável, mas no lugar errado
17-
System.out.println("Exceção capturada: " + cce);
12+
} catch (NullPointerException | IllegalArgumentException e) { // COMPILES - multiple exceptions in the same catch, only one variable at the end
13+
System.out.println("Exception caught: " + e);
14+
} catch (IllegalStateException ise | UnsupportedOperationException uoe) { // NOT COMPILING - more than one declared variable
15+
System.out.println("Exception caught: " + ise);
16+
} catch (ClassCastException cce | ConcurrentModificationException) { // NOT COMPILING - just one variable but in the wrong place
17+
System.out.println("Exception caught: " + cce);
1818
}
1919
}
2020
// end::code[]

src/org/j6toj8/languageenhancements/multipleexception/MultipleException_OverrideVar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public static void main(String[] args) {
88
try {
99
throw new NullPointerException();
1010
} catch (NullPointerException | IllegalArgumentException e) {
11-
e = new IllegalStateException(); // NÃO COMPILA - a variável não pode ser sobrescrita quando está em um multi-catch
11+
e = new IllegalStateException(); // NOT COMPILING - variable cannot be overwritten when multi-catching
1212
} catch (Exception e) {
13-
e = new IllegalStateException(); // COMPILA - ainda é possível sobrescrever a variável quando não é um multi-catch
13+
e = new IllegalStateException(); // COMPILES - it is still possible to overwrite the variable when it is not a multi-catch
1414
}
1515
}
1616
// end::code[]

src/org/j6toj8/languageenhancements/multipleexception/MultipleException_Redundant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public static void main(String[] args) {
99

1010
try {
1111
throw new NullPointerException();
12-
} catch (RuntimeException | IllegalArgumentException e) { // NÃO COMPILA - IllegalArgumentException herda de RuntimeException, logo seria redundante
13-
System.out.println("Exceção capturada: " + e);
12+
} catch (RuntimeException | IllegalArgumentException e) { // NOT COMPILING - IllegalArgumentException inherits from RuntimeException, so would be redundant
13+
System.out.println("Exception caught: " + e);
1414
}
1515
}
1616
// end::code[]

src/org/j6toj8/languageenhancements/multipleexception/MultipleException_RepeatException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public static void main(String[] args) {
88
try {
99
throw new NullPointerException();
1010
} catch (NullPointerException | IllegalArgumentException e) {
11-
System.out.println("Exceção capturada: " + e);
12-
} catch (IllegalStateException | NullPointerException e) { // NÃO COMPILA - NullPointerException já foi capturada no catch anterior
13-
System.out.println("Exceção capturada: " + e);
11+
System.out.println("Exception caught: " + e);
12+
} catch (IllegalStateException | NullPointerException e) { // NOT COMPILING - NullPointerException already caught in previous catch
13+
System.out.println("Exception caught: " + e);
1414
}
1515
}
1616
// end::code[]

0 commit comments

Comments
 (0)