Last active
April 13, 2017 02:30
-
-
Save fkenjikamei/72a15f97bd852cc1409c to your computer and use it in GitHub Desktop.
Classe em que demostramos como tratar e lançar exceções
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CaixaEletronico { | |
public void emitirDinheiro(double valor) { | |
System.out.println("Retire o dinheiro: " + valor); | |
System.out.println("Saque efetuado com sucesso!"); | |
} | |
public static void main(String[] args) { | |
Conta conta = new Conta(); | |
conta.depositar(200.0); | |
double valorSaque = 5000.0; | |
try { | |
conta.sacar(valorSaque); | |
CaixaEletronico caixa = new CaixaEletronico(); | |
caixa.emitirDinheiro(valorSaque); | |
}catch(RuntimeException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Conta implements IConta { | |
private double saldo; | |
private String nomeCorrentista; | |
@Override | |
public boolean depositar(double valor) { | |
if (valor >= 0) { | |
this.saldo += valor; | |
return true; | |
} else { | |
return false; | |
} | |
} | |
@Override | |
public double getSaldo() { | |
return this.saldo; | |
} | |
@Override | |
public void sacar(double valor) throws RuntimeException { | |
if(valor < this.saldo) { | |
this.saldo -= valor; | |
} | |
else { | |
throw new RuntimeException("Saldo insuficiente"); | |
} | |
} | |
@Override | |
public void setNomeCorrentista(String nome) { | |
this.nomeCorrentista = nome; | |
} | |
@Override | |
public String getNomeCorrentista() { | |
return this.nomeCorrentista; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IConta { | |
public boolean depositar(double valor); | |
public double getSaldo(); | |
public void sacar(double valor) throws RuntimeException; | |
public void setNomeCorrentista(String nome); | |
public String getNomeCorrentista(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OperacoesMatematicas { | |
void dividir(int num1, int num2) { | |
System.out.println(num1/num2); | |
} | |
public static void main(String[] args) { | |
OperacoesMatematicas op = new OperacoesMatematicas(); | |
try { | |
op.dividir(10, 0); | |
} catch(Exception e) { | |
System.out.println("Erro: "+e.getMessage()); | |
} | |
System.out.println("Outra coisa depois"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestaConta { | |
public static void main(String[] args) { | |
Conta conta = new Conta(); | |
conta.depositar(200.0); | |
double valorSaque = 5000.0; | |
conta.sacar(valorSaque); | |
CaixaEletronico caixa = new CaixaEletronico(); | |
caixa.emitirDinheiro(valorSaque); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestaExcecao { | |
private void m1() { | |
System.out.println("inicio m1"); | |
m2(); | |
System.out.println("fim m1"); | |
} | |
private void m2() { | |
System.out.println("inicio m2"); | |
int[] numeros = new int[5]; | |
for (int i = 0; i <= numeros.length; i++) { | |
numeros[i] = i; | |
System.out.println(numeros[i]); | |
} | |
System.out.println("fim m2"); | |
} | |
public static void main(String[] args) { | |
TestaExcecao testa = new TestaExcecao(); | |
System.out.println("inicio man"); | |
try { | |
testa.m1(); | |
}catch(Exception e) { | |
System.out.println("Erro em main: "+e.getMessage()); | |
} | |
System.out.println("fim man"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment