Skip to content

Commit e1f3667

Browse files
committed
unchecked personalizada
1 parent 4012911 commit e1f3667

File tree

7 files changed

+78
-58
lines changed

7 files changed

+78
-58
lines changed
Binary file not shown.
Binary file not shown.

src/br/com/dio/exceptions/ExceptionCustomizada_1.java

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,67 @@
44
import java.io.*;
55

66
public class ExceptionCustomizada_1 {
7-
/*public static void main(String[] args) {
8-
String nomeDoArquivo = "romances-blake-crouch.txt";
9-
imprimeArquivoNoConsole(nomeDoArquivo);
10-
System.out.println("Vai chegar nessa linha independente de qualquer coisa!");
7+
public static void main(String[] args) {
8+
String nomeDoArquivo = JOptionPane.showInputDialog("Nome do arquivo a ser exibido: ");
9+
10+
imprimirArquivoNoConsole(nomeDoArquivo);
11+
System.out.println("\nCom exception ou não, o programa continua...");
1112
}
1213

13-
private static void imprimeArquivoNoConsole(String nomeDoArquivo) {
14-
File file = new File(nomeDoArquivo);
14+
public static void imprimirArquivoNoConsole(String nomeDoArquivo) {
1515

16-
try{
17-
BufferedReader br = lerArquivo(file.getName());
16+
try {
17+
BufferedReader br = lerArquivo(nomeDoArquivo);
1818
String line = br.readLine();
19-
2019
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
21-
22-
do{
20+
do {
2321
bw.write(line);
2422
bw.newLine();
25-
line=br.readLine();
26-
} while(line != null);
23+
line = br.readLine();
24+
} while (line != null);
2725
bw.flush();
28-
} catch (FileNotFoundException e) {
29-
JOptionPane.showMessageDialog(null, "Arquivo não existe: " + e.getMessage());
30-
e.printStackTrace();
31-
} catch (IOException e) {
26+
br.close();
27+
} catch (ImpossivelAberturaDeArquivoException e) {
28+
JOptionPane.showMessageDialog(null,
29+
e.getMessage());
3230
e.printStackTrace();
31+
} catch (IOException ex) {
32+
JOptionPane.showMessageDialog(null,
33+
"Ocorreu um erro não esperado, por favor, fale com o suporte." + ex.getMessage());
34+
ex.printStackTrace();
3335
}
3436
}
3537

36-
private static BufferedReader lerArquivo(String nomeDoArquivo) {
37-
return new BufferedReader(new FileReader(nomeDoArquivo));
38+
public static BufferedReader lerArquivo(String nomeDoArquivo) throws ImpossivelAberturaDeArquivoException {
39+
File file = new File(nomeDoArquivo);
40+
try {
41+
return new BufferedReader(new FileReader(nomeDoArquivo));
42+
} catch (FileNotFoundException e) {
43+
throw new ImpossivelAberturaDeArquivoException(file.getName(), file.getPath());
44+
}
45+
}
46+
}
47+
48+
class ImpossivelAberturaDeArquivoException extends Exception {
49+
50+
private String nomeDoArquivo;
51+
private String diretorio;
52+
53+
public ImpossivelAberturaDeArquivoException(String nomeDoArquivo, String diretorio) {
54+
super("O arquivo " + nomeDoArquivo + " não foi encontrado no diretório " + diretorio);
55+
this.nomeDoArquivo = nomeDoArquivo;
56+
this.diretorio = diretorio;
57+
}
58+
59+
/*@Override
60+
public String toString() {
61+
return "ImpossivelAberturaDeArquivoException{" +
62+
"nomeDoArquivo='" + nomeDoArquivo + '\'' +
63+
", diretorio='" + diretorio + '\'' +
64+
'}';
3865
}*/
3966

4067
}
68+
69+
70+
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
package br.com.dio.exceptions;
22

3+
import javax.swing.*;
4+
35
public class ExceptionCustomizada_2 {
46
public static void main(String[] args) {
5-
int[] numerador = {4, 2, 5, 8, 10};
6-
int[] denominador = {2, 0, 4, 0, 2, 8};
7+
int[] numerador = {4, 5, 8, 10};
8+
int[] denominador = {2, 4, 0, 2, 8};
79

810
for(int i = 0; i < denominador.length; i++) {
11+
try {
12+
if(numerador[i] % 2 !=0) throw new DivisaoNaoExataException("Divisão não exata!", numerador[i]);
13+
914
int resultado = numerador[i] / denominador[i];
1015
System.out.println(resultado);
16+
} catch (ArithmeticException | IndexOutOfBoundsException | DivisaoNaoExataException e) {
17+
e.printStackTrace();
18+
JOptionPane.showMessageDialog(null, e.getMessage());
19+
}
1120
}
21+
System.out.println("O programa continua...");
1222
}
13-
1423
}
1524

25+
class DivisaoNaoExataException extends Exception {
26+
27+
private int numerador;
28+
29+
public DivisaoNaoExataException(String message, int numerador) {
30+
super(message);
31+
this.numerador = numerador;
32+
}
33+
}

src/test/ExceptionCustomizada.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.io.*;
55

66
public class ExceptionCustomizada {
7-
public static void main(String[] args) {
7+
/*public static void main(String[] args) {
88
String nomeDoArquivo = JOptionPane.showInputDialog("Nome do arquivo a ser exibido: ");
99
1010
imprimirArquivoNoConsole(nomeDoArquivo);
@@ -25,22 +25,14 @@ public static void imprimirArquivoNoConsole(String nomeDoArquivo) {
2525
bw.flush();
2626
br.close();
2727
} catch (IOException ex) {
28-
JOptionPane.showMessageDialog(null, "Ocorreu um erro não esperado, por favor, fale com o suporte." + ex.getMessage());
28+
JOptionPane.showMessageDialog(null,
29+
"Ocorreu um erro não esperado, por favor, fale com o suporte." + ex.getMessage());
2930
ex.printStackTrace();
30-
} catch (NaoFoiPossivelAbrirOArquivoException e) {
31-
JOptionPane.showMessageDialog(null, e.getMessage());
32-
e.printStackTrace();
3331
}
3432
}
3533
36-
public static BufferedReader lerArquivo(String nomeDoArquivo) throws NaoFoiPossivelAbrirOArquivoException {
34+
public static BufferedReader lerArquivo(String nomeDoArquivo) {
3735
File file = new File(nomeDoArquivo);
38-
39-
try {
40-
return new BufferedReader(new FileReader(nomeDoArquivo));
41-
} catch (FileNotFoundException e) {
42-
throw new NaoFoiPossivelAbrirOArquivoException(file.getName(), file.getPath());
43-
}
44-
45-
}
36+
return new BufferedReader(new FileReader(nomeDoArquivo));
37+
}*/
4638
}

src/test/ExceptionCustomizada_2.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,13 @@
11
package test;
22

3-
import javax.swing.*;
4-
53
public class ExceptionCustomizada_2 {
64
public static void main(String[] args) {
7-
int[] numerador = {4, 2, 5, 8, 10};
8-
int[] denominador = {2, 0, 4, 0, 2, 8};
5+
int[] numerador = {4, 5, 2, 8, 10};
6+
int[] denominador = {2, 4, 6, 0, 2, 8};
97

108
for(int i = 0; i < denominador.length; i++) {
11-
try{
12-
if(numerador[i] % 2 != 0) throw new DivisaoNaoExataException(numerador[i]);
13-
149
int resultado = numerador[i] / denominador[i];
1510
System.out.println(resultado);
16-
} catch (DivisaoNaoExataException | ArithmeticException | ArrayIndexOutOfBoundsException e) {
17-
JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
18-
e.printStackTrace();
19-
}
2011
}
2112
}
22-
2313
}
24-
25-
class DivisaoNaoExataException extends Exception {
26-
private int numerador;
27-
28-
public DivisaoNaoExataException(int numerador) {
29-
super("O valor " + numerador + "é ímpar, por tanto, divisão não exata!");
30-
this.numerador = numerador;
31-
}
32-
}

src/test/NaoFoiPossivelAbrirOArquivoException.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ public NaoFoiPossivelAbrirOArquivoException(String nomeDoArquivo, String diretor
1111
this.diretorioDoArquivo = diretorioDoArquivo;
1212
}
1313

14-
1514
}

0 commit comments

Comments
 (0)