Monday, November 11, 2013

Treinamento Java 2 Básico - Capítulo 3 Tratamento de erros



Pág 56 capítulo 3 Tratamento de erros

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package visao;

  6. /**
  7.  *
  8.  * @author Paulo Henrique Cayres
  9.  */
  10. public class ExException01 {

  11.     public static void main(String args[])
  12.     {
  13.         String frase = null;
  14.         String novaFrase = null;
  15.         novaFrase = frase.toUpperCase();
  16.         System.out.println("Frase antiga: "+frase);
  17.         System.out.println("Frase nova: "+novaFrase);
  18.     }

  19. }


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package visao;

  6. /**
  7.  *
  8.  * @author Paulo Henrique Cayres
  9.  */
  10. public class ExException02 {
  11.     
  12.     public static void main(String args[])
  13.     {
  14.         String frase = null;
  15.         String novaFrase = null;
  16.         try
  17.         {
  18.             novaFrase = frase.toUpperCase();
  19.         }
  20.         catch(NullPointerException e) //CAPTURA DA POSSÍVEL exceção.
  21.         {
  22.             //TRATAMENTO DA exceção
  23.             System.out.println("A frase inicial está nula, para solucionar tal o problema, foi lhe atribuito um valor default.");
  24.             frase = "Frase vazia";
  25.             novaFrase = frase.toUpperCase();
  26.         }
  27.         System.out.println("Frase antiga: "+frase);
  28.         System.out.println("Frase nova: "+novaFrase);
  29.     }
  30.     
  31. }




xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package visao;

  6. /**
  7.  *
  8.  * @author Paulo Henrique Cayres
  9.  */
  10. public class ExException03 {

  11.     public static void main(String args[])
  12.     {
  13.         String frase = null;
  14.         String novaFrase = null;
  15.         try
  16.         {
  17.             novaFrase = frase.toLowerCase();
  18.         }
  19.         catch(NullPointerException e)
  20.         {
  21.             System.out.println("A frase inicial está nula, para solucionar tal o problema, foi lhe atribuito um valor default.");
  22.             frase = "Fras vazia uai";
  23.         }
  24.         finally
  25.         {
  26.             novaFrase = frase.toUpperCase();
  27.         }
  28.         System.out.println("Frase antiga: "+frase);
  29.         System.out.println("Frase nova: "+novaFrase);
  30.     }

  31. }


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package visao;

  6. /**
  7.  *
  8.  * @author Paulo Henrique Cayres
  9.  */
  10. public class ExException04 {

  11.     private static void aumentarLetras() throws NullPointerException //lançando excessão
  12.     {
  13.         String frase = null;
  14.         String novaFrase = null;
  15.         novaFrase = frase.toUpperCase();
  16.         System.out.println("Frase antiga: "+frase);
  17.         System.out.println("Frase nova: "+novaFrase);
  18.     }
  19.  
  20.     public static void main(String args[])
  21.     {
  22.         try
  23.         {
  24.             aumentarLetras();
  25.         }
  26.         catch(NullPointerException e)
  27.         {
  28.             System.out.println("Ocorreu um NullPointerException ao executar o método aumentarLetras() "+e);
  29.         }
  30.     }

  31. }



xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package visao;

  6. /**
  7.  *
  8.  * @author paulo983
  9.  */
  10. public class ExException05 {
  11.     
  12.     private static void aumentarLetras() throws Exception //lançando exceção
  13.     {
  14.         String frase = null;
  15.         String novaFrase = null;
  16.         try
  17.         {
  18.             novaFrase = frase.toUpperCase();
  19.         }
  20.         catch(NullPointerException e)
  21.         {
  22.             throw new Exception(e);
  23.         }
  24.         System.out.println("Frase antiga: "+frase);
  25.         System.out.println("Frase nova: "+novaFrase);
  26.     }
  27.     public static void main(String args[])
  28.     {
  29.         try
  30.         {
  31.             aumentarLetras();
  32.         }
  33.         catch(Exception e)
  34.         {
  35.             System.out.println("Ocorreu uma exceção ao executar o método aumentarLetras() "+e);
  36.         }
  37.     }

  38. }



xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package visao;

  6. import controle.SemLetraBException;

  7. /**
  8.  *
  9.  * @author Paulo Henrique Cayres
  10.  */
  11. public class ExException06 {
  12.     
  13.     public static void main(String args[]) throws SemLetraBException
  14.     {
  15.         String frase = "Sou um teste!";
  16.         if(!frase.contains("b") || !frase.contains("B"))
  17.             throw new SemLetraBException();
  18.     }

  19. }




Segue a exceção que foi criada:


  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package controle;

  6. /**
  7.  *
  8.  * @author paulo983
  9.  */
  10. public class SemLetraBException extends Exception {
  11.     
  12.     /**
  13.      * 
  14.      * @return 
  15.      */
  16.     @Override
  17.     public String toString(){
  18.         return "Não existe letra B em sua frase";
  19.     }

  20. }



xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package visao;

  6. import controle.DivPorZero;
  7. import modelo.Calculadora;

  8. /**
  9.  *
  10.  * @author paulo983
  11.  */
  12. public class ExException07 {
  13.     
  14.     public static void main(String[] args){
  15.         
  16.         try{
  17.             Calculadora calc = new Calculadora();
  18.             System.out.println("Resultado da divisão: "+  calc.calculaMedia(12.0, 0.0));
  19.         }
  20.         catch(DivPorZero e){
  21.             System.out.println(e);
  22.         }
  23.     }
  24.         
  25. }

Classe Calculadora:


  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package modelo;

  6. import controle.DivPorZero;

  7. /**
  8.  *
  9.  * @author Paulo Henrique Cayres
  10.  */
  11. public class Calculadora {

  12.     public double calculaMedia(double a, double b) throws DivPorZero {
  13.         if (b == 0) {
  14.             throw new DivPorZero();
  15.         }
  16.         
  17.         return a/b;
  18.     }
  19.     
  20. }

Classe DivPorZero:

  1. package controle;

  2. /**
  3.  *
  4.  * @author Paulo Henrique Cayres
  5.  */
  6. public class DivPorZero extends Exception {
  7.     
  8.     /**
  9.      * 
  10.      * @return - Mensagem de erro de divisão por zero.
  11.      */
  12.     @Override
  13.     public String toString() {
  14.         return "Erro de divisão por zero!";
  15.     }    
  16. }



xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package visao;

  6. import controle.ErroStringVasia;
  7. import modelo.TestaString;

  8. /**
  9.  *
  10.  * @author paulo983
  11.  */
  12. public class ExTestaString {

  13.     public static void main(String[] args) {
  14.      
  15.         TestaString teste = new TestaString();
  16.         try {
  17.             double num = teste.retornaDouble(javax.swing.JOptionPane.showInputDialog("Digite um número"));
  18.             javax.swing.JOptionPane.showMessageDialog(null, "Numero digitado: "+num, "Mensagem", javax.swing.JOptionPane.INFORMATION_MESSAGE);
  19.             
  20.         } catch (ErroStringVasia ex) {
  21.             javax.swing.JOptionPane.showMessageDialog(null, ex, "Mensagem",javax.swing.JOptionPane.ERROR_MESSAGE);
  22.         }
  23.         catch (NumberFormatException ex){
  24.             javax.swing.JOptionPane.showMessageDialog(null, ex, "Mensagem",javax.swing.JOptionPane.ERROR_MESSAGE);            
  25.         }
  26.     }            
  27. }

Classe ErroStringVasia:

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package controle;

  6. /**
  7.  *
  8.  * @author Paulo Henrique Cayres
  9.  */
  10. public class ErroStringVasia extends Exception {

  11.     /**
  12.      * 
  13.      * @return Mensagem de erro de conversão de tipos
  14.      */
  15.     @Override
  16.     public String toString() {
  17.         return "Impossível converter. Favor informar apenas dígitos na entrada de dados!";
  18.     }
  19.     
  20. }
Classe TestaString:

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package modelo;

  6. import controle.ErroStringVasia;

  7. /**
  8.  *
  9.  * @author paulo983
  10.  */
  11. public class TestaString {

  12.     public double retornaDouble(String temp) throws ErroStringVasia {
  13.         if (temp.isEmpty()) throw new ErroStringVasia();
  14.         return Double.parseDouble(temp);
  15.     }
  16. }







xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package visao;

  6. /**
  7.  *
  8.  * @author Aluno
  9.  */
  10. public class ExemploExceptionMain {
  11.     public static void main(String[] args){
  12.         System.out.println(args[0]);
  13.     }
  14.     
  15. }




No comments:

Post a Comment