4. Problema 2 // ... Conta minhaConta = new Conta(); minhaConta.deposita(100); minhaConta.setLimite(100); minhaConta.saca(1000); // o saldo é -900? É 100? É 0? // A chamada ao método saca funcionou?
5.
6.
7.
8.
9.
10. Pilha de Execução public class TesteErro { public static void main(String[] args) { System. out .println( "inicio do main" ); metodo1 (); System. out .println( "fim do main" ); } public static void metodo1() { System. out .println( "inicio do metodo1" ); metodo2 (); System. out .println( "fim do metodo1" ); } public static void metodo2() { System. out .println( "inicio do metodo2" ); int [] array = new int [10]; for ( int i = 0; i <= 15; i++) { array[i] = i; System. out .println(i); } System. out .println( "fim do metodo2" ); } }
11. Rastro da Pilha - Stacktrace Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException : 10 at TesteErro.metodo2( TesteErro.java:18 ) at TesteErro.metodo1( TesteErro.java:10 ) at TesteErro.main( TesteErro.java:4 )
12. Soluções try { for ( int i = 0; i <= 15; i++) { array[i] = i; System.out.println(i); } } catch (ArrayIndexOutOfBoundsException e) { System.out.println( "erro: " + e); } for ( int i = 0; i <= 15; i++) { try { array[i] = i; System.out.println(i); } catch (ArrayIndexOutOfBoundsException e) { System.out.println( "erro: " + e); } }
17. Exceções não checadas public class Exemplo09 { public static void main(String args[]) { int i = 5571; i = i / 0; System. out .println( "O resultado " + i); } } Exception in thread "main" java.lang.ArithmeticException : / by zero at Exemplo09.main( Exemplo09.java:4 )
18. Exceções não checadas public class Exemplo09 { public static void main(String args[]) { Conta c = null ; System. out .println( "Saldo: " + c.getSaldo()); } } Exception in thread "main" java.lang.NullPointerException at Exemplo09.main( Exemplo09.java:4 )
19. Exceções não checadas public class Exemplo09 { public static void main(String args[]) { int i = Integer. parseInt ( "a" ); System. out .println(i); } } Exception in thread "main" java.lang.NumberFormatException : For input string: "a" at java.lang.NumberFormatException.forInputString( NumberFormatException.java:48 ) at java.lang.Integer.parseInt( Integer.java:447 ) at java.lang.Integer.parseInt( Integer.java:497 ) at Exemplo09.main( Exemplo09.java:3 )
20.
21. Exceções checadas import java.io.FileReader; public class Exemplo09 { public static void main(String args[]) { FileReader f = new FileReader( "teste.txt" ); } } Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type FileNotFoundException at Exemplo09.main( Exemplo09.java:5 )
22. Tratamento de Exceções - catch import java.io.FileNotFoundException; import java.io.FileReader; public class Exemplo09 { public static void main(String args[]) { try { FileReader f = new FileReader( "teste.txt" ); } catch (FileNotFoundException e) { System. out .println( "Arquivo não existe" ); } } }
23. Propagação de Exceções - throws import java.io.FileNotFoundException; import java.io.FileReader; public class Exemplo09 { public static void main(String args[]) throws FileNotFoundException { FileReader f = new FileReader( "teste.txt" ); } }
27. Lançamento de Exceções public class Conta { double saldo ; // ... public boolean saca( double valor){ if ( this . saldo < valor){ return false ; } else { this . saldo -=valor; return true ; } } // ... }
28. Lançamento de Exceções public class Conta { double saldo ; // ... public void saca( double valor){ if ( this . saldo < valor){ throw new IllegalArgumentException( "Saldo insuficiente" ); } else { this . saldo -=valor; } } // ... }
29.
30. Criando Exceções public class SaldoInsuficienteException extends RuntimeException { public SaldoInsuficienteException(String message) { super (message); } }
31. Usando nossas exceções public class Conta { double saldo ; // ... public void saca( double valor){ if ( this . saldo < valor){ throw new SaldoInsuficienteException( "Use um valor menor" ); } else { this . saldo -=valor; } } // ... } // ... Conta cc = new ContaCorrente(); cc.deposita(100); try { cc .saca(100); } catch (SaldoInsuficienteException e){ System.out.println(e.getMessage()); } // ...