52 Curso de Java
Criando sua
própria
Exception
Hands On!
public class NaoInteiroException extends Exception {
protected int num;
protected int denom;
public NaoInteiroException(int num, int denom) {
super();
this.num = num;
this.denom = denom;
}
@Override
public String toString() {
return "Resultado de " + num + "/" + denom + " não é inteiro";
}
}
public class NaoInteiroException extends Exception {
protected int num;
protected int denom;
public NaoInteiroException(int num, int denom) {
super();
this.num = num;
this.denom = denom;
}
@Override
public String toString() {
return "Resultado de " + num + "/" + denom + " não é inteiro";
}
}
public class MultiCatch {
public static void main(String[] args) {
int[] numero = {4, 8, 16, 21, 32, 64, 128};
int[] denom = {2, 0, 4, 8, 0};
for (int i=0; i<numero.length; i++){
try{
if (numero[i] % 2 != 0){
throw new NaoInteiroException(numero[i], denom[i]);
}
System.out.println(numero[i] + "/" + denom[i] + " = " + (numero[i]/denom[i]));
}
catch (ArithmeticException | ArrayIndexOutOfBoundsException | NaoInteiroException e){
e.printStackTrace();
}
}
}
}
Usar: palavra chave throw
public class MultiCatch {
public static void main(String[] args) {
int[] numero = {4, 8, 16, 21, 32, 64, 128};
int[] denom = {2, 0, 4, 8, 0};
for (int i=0; i<numero.length; i++){
try{
if (numero[i] % 2 != 0){
throw new NaoInteiroException(numero[i], denom[i]);
}
System.out.println(numero[i] + "/" + denom[i] + " = " + (numero[i]/denom[i]));
}
catch (ArithmeticException | ArrayIndexOutOfBoundsException | NaoInteiroException e){
e.printStackTrace();
}
}
}
}
Usar: palavra chave throw
Lição
de casa
Lista de exercícios aulas 47 a 52
http://www.slideshare.net/loianeg/curso-java-
basico-exercicios-aulas-47-a-52
https://github.com/loiane/curso-java-basico
Código Fonte:
Não conhece Git/Github?
http://www.loiane.com/2013/11/
screencast-git-e-github-para-iniciantes
http://loiane.training
Fórum para dúvidas + certificado do curso.
Cadastro em:
http://loiane.com
facebook.com/loianegroner
@loiane
https://github.com/loiane
youtube.com/user/Loianeg
Obrigada!
http://loiane.com

[Curso Java Basico - Exceptions] Aula 52: criando sua propria exception