Skip to content

Commit

Permalink
feat(lang-enh): 馃幐 handles multiple Exception, translated
Browse files Browse the repository at this point in the history
Refers: #4
  • Loading branch information
rcmoutinho committed Sep 11, 2019
1 parent f76ca26 commit 81639b9
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 38 deletions.
34 changes: 16 additions & 18 deletions book/02-language-enhancement/sections/03-multiple-exception.asc
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
:java-package: src/org/j6toj8/languageenhancements
:section-java-package: ../../../{java-package}

=== M煤ltiplas `Exception` no mesmo `catch`
=== Handles multiple Exception in a single catch

.Objetivo
.Objective
----
Develop code that handles multiple Exception types in a single catch block.
-
Desenvolver c贸digo que lide com m煤ltiplos tipos de Exception em um 煤nico bloco catch.
----

脡 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`.
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.

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.
Before proceeding, based on the following example, understand the execution of the `main` method and what is presented on the console after its execution.

[source,java,indent=0]
.{java-package}/multipleexception/MultipleException_Complete.java
----
include::{section-java-package}/multipleexception/MultipleException_Complete.java[tag=code]
----

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

.Sa铆da no console
.console output
[source,console]
----
Exce莽茫o capturada: java.lang.NullPointerException
Exception caught: java.lang.NullPointerException
----

. Desde o Java 7, m煤ltiplas exce莽玫es podem ser capturadas no mesmo `catch`.
. Since Java 7, multiple exceptions can be caught in the same catch.

. Apenas uma vari谩vel 茅 permitida para um bloco `catch`, e deve estar localizada no final.
. Only one variable is allowed for a `catch` block, and must be located at the end.
+
[source,java,indent=0]
.{java-package}/multipleexception/MultipleException_MultipleSameCatch.java
----
include::{section-java-package}/multipleexception/MultipleException_MultipleSameCatch.java[tag=code]
----

. N茫o 茅 permitido declarar exce莽玫es diferentes, mas que seriam redundantes levando em considera莽茫o a heran莽a.
. It is not allowed to declare different exceptions, but would be redundant considering inheritance.

+
[source,java,indent=0]
Expand All @@ -47,15 +45,15 @@ include::{section-java-package}/multipleexception/MultipleException_MultipleSame
include::{section-java-package}/multipleexception/MultipleException_Redundant.java[tag=code]
----

. 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`.
. 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`.
+
[source,java,indent=0]
.{java-package}/multipleexception/MultipleException_OverrideVar.java
----
include::{section-java-package}/multipleexception/MultipleException_OverrideVar.java[tag=code]
----

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

+
[source,java,indent=0]
Expand All @@ -64,26 +62,26 @@ include::{section-java-package}/multipleexception/MultipleException_OverrideVar.
include::{section-java-package}/multipleexception/MultipleException_GenericsLower.java[tag=code]
----

. Assim como nas vers玫es anteriores, Exce莽玫es repetidas ainda s茫o proibidas.
. As in previous versions, repeated exceptions are still prohibited.
+
[source,java,indent=0]
.{java-package}/multipleexception/MultipleException_RepeatException.java
----
include::{section-java-package}/multipleexception/MultipleException_RepeatException.java[tag=code]
----

. 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.
. As in previous versions, Checked Exceptions (those that inherit from `Exception`) can only be in a `catch` if something in `try` throws them.
+
[source,java,indent=0]
.{java-package}/multipleexception/MultipleException_CheckedException.java
----
include::{section-java-package}/multipleexception/MultipleException_CheckedException.java[tag=code]
----

.Refer锚ncias
.References
****
* Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 291). Wiley. Edi莽茫o do Kindle.
* Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 291). Wiley. Kindle Edition.
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public static void main(String[] args) {

try {
throw new NullPointerException();
} catch (NullPointerException | IOException e) { // N脙O COMPILA - IOException n茫o 茅 lan莽ada dentro do bloco try
System.out.println("Exce莽茫o capturada: " + e);
} catch (NullPointerException | IOException e) { // NOT COMPILING - IOException is not thrown inside try block
System.out.println("Exception caught: " + e);
}
}
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public static void main(String[] args) {
try {
throw new NullPointerException();
} catch (NullPointerException | IllegalArgumentException | IllegalStateException e) {
System.out.println("Exce莽茫o capturada: " + e);
System.out.println("Exception caught: " + e);
} catch (Exception e) {
System.out.println("Exce莽茫o capturada: " + e);
System.out.println("Exception caught: " + e);
}
}
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public static void main(String[] args) {
try {
throw new NullPointerException();
} catch (Exception e) {
System.out.println("Exce莽茫o capturada: " + e);
} catch (NullPointerException | IllegalArgumentException e) { // N脙O COMPILA - NullPointerException 茅 mais espec铆fico que Exception, logo deveria ser capturada antes de Exception
System.out.println("Exce莽茫o capturada: " + e);
System.out.println("Exception caught: " + e);
} catch (NullPointerException | IllegalArgumentException e) { // NOT COMPILING - NullPointerException is more specific than Exception, so it should be caught before Exception
System.out.println("Exception caught: " + e);
}
}
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public static void main(String[] args) {

try {
throw new NullPointerException();
} catch (NullPointerException | IllegalArgumentException e) { // COMPILA - m煤ltiplas exce莽玫es no mesmo catch, s贸 uma vari谩vel no final
System.out.println("Exce莽茫o capturada: " + e);
} catch (IllegalStateException ise | UnsupportedOperationException uoe) { // N脙O COMPILA - mais de uma vari谩vel declarada
System.out.println("Exce莽茫o capturada: " + ise);
} catch (ClassCastException cce | ConcurrentModificationException) { // N脙O COMPILA - s贸 uma vari谩vel, mas no lugar errado
System.out.println("Exce莽茫o capturada: " + cce);
} catch (NullPointerException | IllegalArgumentException e) { // COMPILES - multiple exceptions in the same catch, only one variable at the end
System.out.println("Exception caught: " + e);
} catch (IllegalStateException ise | UnsupportedOperationException uoe) { // NOT COMPILING - more than one declared variable
System.out.println("Exception caught: " + ise);
} catch (ClassCastException cce | ConcurrentModificationException) { // NOT COMPILING - just one variable but in the wrong place
System.out.println("Exception caught: " + cce);
}
}
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public static void main(String[] args) {
try {
throw new NullPointerException();
} catch (NullPointerException | IllegalArgumentException e) {
e = new IllegalStateException(); // N脙O COMPILA - a vari谩vel n茫o pode ser sobrescrita quando est谩 em um multi-catch
e = new IllegalStateException(); // NOT COMPILING - variable cannot be overwritten when multi-catching
} catch (Exception e) {
e = new IllegalStateException(); // COMPILA - ainda 茅 poss铆vel sobrescrever a vari谩vel quando n茫o 茅 um multi-catch
e = new IllegalStateException(); // COMPILES - it is still possible to overwrite the variable when it is not a multi-catch
}
}
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public static void main(String[] args) {

try {
throw new NullPointerException();
} catch (RuntimeException | IllegalArgumentException e) { // N脙O COMPILA - IllegalArgumentException herda de RuntimeException, logo seria redundante
System.out.println("Exce莽茫o capturada: " + e);
} catch (RuntimeException | IllegalArgumentException e) { // NOT COMPILING - IllegalArgumentException inherits from RuntimeException, so would be redundant
System.out.println("Exception caught: " + e);
}
}
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public static void main(String[] args) {
try {
throw new NullPointerException();
} catch (NullPointerException | IllegalArgumentException e) {
System.out.println("Exce莽茫o capturada: " + e);
} catch (IllegalStateException | NullPointerException e) { // N脙O COMPILA - NullPointerException j谩 foi capturada no catch anterior
System.out.println("Exce莽茫o capturada: " + e);
System.out.println("Exception caught: " + e);
} catch (IllegalStateException | NullPointerException e) { // NOT COMPILING - NullPointerException already caught in previous catch
System.out.println("Exception caught: " + e);
}
}
// end::code[]
Expand Down

0 comments on commit 81639b9

Please sign in to comment.