Skip to content

Ajuste na exceção divisão não exata e Implementação de duas outras exceções personalizadas. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/br/com/dio/exceptions/DivisaoPorZeroException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package br.com.dio.exceptions;

public class DivisaoPorZeroException extends Exception {

private int denominador;

public DivisaoPorZeroException(String message, int denominador) {
super(message);
this.denominador = denominador;
}

}
24 changes: 19 additions & 5 deletions src/br/com/dio/exceptions/ExceptionCustomizada_2.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
package br.com.dio.exceptions;

import javax.swing.*;
import java.util.Arrays;

public class ExceptionCustomizada_2 {
public static void main(String[] args){
int[] numerador = {4, 5, 8, 10};
int[] denominador = {2, 4, 0, 2, 8};


for (int i = 0; i < denominador.length; i++) {
try {
if(numerador[i] %2 != 0)
throw new DivisaoNaoExataException("Divisão não exata!", numerador[i], denominador[i]);
try{
if(denominador[i] == 0)
throw new DivisaoPorZeroException("Divisao por Zero",denominador[i]);
try {
if (numerador[i] % denominador[i] != 0)
throw new DivisaoNaoExataException("Divisão não exata!", numerador[i], denominador[i]);
}catch(ArrayIndexOutOfBoundsException e){
throw new TamanhoDoArrayInvalidoException("Tamanho do Array Invalido", i);
}

int resultado = numerador[i] / denominador[i];
System.out.println(resultado);
} catch (DivisaoNaoExataException | ArithmeticException | ArrayIndexOutOfBoundsException e) {
e.printStackTrace();


} catch (DivisaoNaoExataException | DivisaoPorZeroException | TamanhoDoArrayInvalidoException e) {
// e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage());
}



}


System.out.println("O programa continua...");
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/br/com/dio/exceptions/TamanhoDoArrayInvalidoException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package br.com.dio.exceptions;

public class TamanhoDoArrayInvalidoException extends Exception{

private int i;

public TamanhoDoArrayInvalidoException(String message, int i) {
super(message);
this.i = i;
}
}