Baixe o app para aproveitar ainda mais
Prévia do material em texto
Linguagem de Programação Fabiano Gonçalves dos Santos Aula 9 * Apresentar o conceitos de Exceção e os seus tratamentos Apresentar as estruturas de try – catch Apresentar a estrutura Throw Conteúdo * * Exemplos Existem programas que usam códigos de erro: * * POG: Programação Orientada a Gambiarras * * Tipos de erros de execução Errors x Exceptions * * Checked e Unchecked * * Unchecked public void deposita (double valor){ if( valor<0){ IllegalArgumentException erro = new IllegalArgumentException(); throw erro ; } else { ... } } * * Checked public void deposita (double valor) throws Exception { if(valor<0) { Exception erro = new Exception(); throw erro ; } else { ... * * Capturar Exceptions class Teste { public static void main (String[] args){ Conta c = new Conta(); try { c.deposita(100); } catch(IllegalArgumentExceptione) { System.out.println("Houve um erro ao depositar"); } } } * * class Teste { public static void main(String[] args){ Conta c = new Conta(); try { c.deposita(100); } catch(IllegalArgumentException e){ System.out.println("Houve uma IllegalArgumentException ao depositar"); } catch (SQLException e) { System.out.println("Houve uma SQLException ao depositar "); } * Capturar Exceptions Linguagem de Programação Fabiano Gonçalves dos Santos Atividade 9 * Ache e trate o erro public class Funcionario { private double salario; public void aumentaSalario(double aumento){ if(aumento<0){ IllegalArgumentException erro = new IllegalArgumentException(); throw erro ; } } // GETTERS E SETTERS } * * class TestaFuncionario { public static void main ( String [] args ) { Funcionario f = new Funcionario (); f. aumentaSalario(-1000) ; } } * Ache e trate o erro * class TestaFuncionario { public static void main (String[] args ) { Funcionario f = new Funcionario(; try { f.aumentaSalario(-1000); } catch (IllegalArgumentException e) { System.out.println("Houve uma IllegalArgumentException ao aumentar o salário"); } } } * Ache e trate o erro * AULA 1 AULA 1
Compartilhar