Skip to content

Commit 0d336a1

Browse files
committed
UncheckedException
1 parent a7d2d62 commit 0d336a1

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed
877 Bytes
Binary file not shown.

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,33 @@
55
//Fazer a divisão de 2 valores inteiros
66
public class UncheckedException {
77
public static void main(String[] args) {
8-
String a = JOptionPane.showInputDialog("Numerador: ");
9-
String b = JOptionPane.showInputDialog("Denominador: ");
108

11-
double resultado = dividir(Integer.parseInt(a), Integer.parseInt(b));
12-
System.out.println("Resultado: " + resultado);
9+
boolean continueLooping = true;
10+
do {
11+
String a = JOptionPane.showInputDialog("Numerador: ");
12+
String b = JOptionPane.showInputDialog("Denominador: ");
13+
14+
try{
15+
int resultado = dividir(Integer.parseInt(a), Integer.parseInt(b));
16+
System.out.println("Resultado: " + resultado);
17+
continueLooping = false;
18+
} catch (NumberFormatException e) {
19+
e.printStackTrace();
20+
JOptionPane.showMessageDialog(null, "Entrada inválida, informe um número inteiro! " + e.getMessage());
21+
} catch (ArithmeticException e) {
22+
e.printStackTrace();
23+
JOptionPane.showMessageDialog(null, "Impossível dividir um número por 0.");
24+
}
25+
finally {
26+
System.out.println("Chegou no finally!");
27+
}
28+
} while(continueLooping);
29+
30+
31+
System.out.println("O código continua...");
1332
}
1433

15-
public static double dividir(int a, int b) {
16-
return (double) a / b;
34+
public static int dividir(int a, int b) {
35+
return a / b;
1736
}
1837
}

0 commit comments

Comments
 (0)