Pág 56 capítulo 3 Tratamento de erros
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package visao;
- /**
- *
- * @author Paulo Henrique Cayres
- */
- public class ExException01 {
- public static void main(String args[])
- {
- String frase = null;
- String novaFrase = null;
- novaFrase = frase.toUpperCase();
- System.out.println("Frase antiga: "+frase);
- System.out.println("Frase nova: "+novaFrase);
- }
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package visao;
- /**
- *
- * @author Paulo Henrique Cayres
- */
- public class ExException02 {
- public static void main(String args[])
- {
- String frase = null;
- String novaFrase = null;
- try
- {
- novaFrase = frase.toUpperCase();
- }
- catch(NullPointerException e) //CAPTURA DA POSSÍVEL exceção.
- {
- //TRATAMENTO DA exceção
- System.out.println("A frase inicial está nula, para solucionar tal o problema, foi lhe atribuito um valor default.");
- frase = "Frase vazia";
- novaFrase = frase.toUpperCase();
- }
- System.out.println("Frase antiga: "+frase);
- System.out.println("Frase nova: "+novaFrase);
- }
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package visao;
- /**
- *
- * @author Paulo Henrique Cayres
- */
- public class ExException03 {
- public static void main(String args[])
- {
- String frase = null;
- String novaFrase = null;
- try
- {
- novaFrase = frase.toLowerCase();
- }
- catch(NullPointerException e)
- {
- System.out.println("A frase inicial está nula, para solucionar tal o problema, foi lhe atribuito um valor default.");
- frase = "Fras vazia uai";
- }
- finally
- {
- novaFrase = frase.toUpperCase();
- }
- System.out.println("Frase antiga: "+frase);
- System.out.println("Frase nova: "+novaFrase);
- }
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package visao;
- /**
- *
- * @author Paulo Henrique Cayres
- */
- public class ExException04 {
- private static void aumentarLetras() throws NullPointerException //lançando excessão
- {
- String frase = null;
- String novaFrase = null;
- novaFrase = frase.toUpperCase();
- System.out.println("Frase antiga: "+frase);
- System.out.println("Frase nova: "+novaFrase);
- }
- public static void main(String args[])
- {
- try
- {
- aumentarLetras();
- }
- catch(NullPointerException e)
- {
- System.out.println("Ocorreu um NullPointerException ao executar o método aumentarLetras() "+e);
- }
- }
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package visao;
- /**
- *
- * @author paulo983
- */
- public class ExException05 {
- private static void aumentarLetras() throws Exception //lançando exceção
- {
- String frase = null;
- String novaFrase = null;
- try
- {
- novaFrase = frase.toUpperCase();
- }
- catch(NullPointerException e)
- {
- throw new Exception(e);
- }
- System.out.println("Frase antiga: "+frase);
- System.out.println("Frase nova: "+novaFrase);
- }
- public static void main(String args[])
- {
- try
- {
- aumentarLetras();
- }
- catch(Exception e)
- {
- System.out.println("Ocorreu uma exceção ao executar o método aumentarLetras() "+e);
- }
- }
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package visao;
- import controle.SemLetraBException;
- /**
- *
- * @author Paulo Henrique Cayres
- */
- public class ExException06 {
- public static void main(String args[]) throws SemLetraBException
- {
- String frase = "Sou um teste!";
- if(!frase.contains("b") || !frase.contains("B"))
- throw new SemLetraBException();
- }
- }
Segue a exceção que foi criada:
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package controle;
- /**
- *
- * @author paulo983
- */
- public class SemLetraBException extends Exception {
- /**
- *
- * @return
- */
- @Override
- public String toString(){
- return "Não existe letra B em sua frase";
- }
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package visao;
- import controle.DivPorZero;
- import modelo.Calculadora;
- /**
- *
- * @author paulo983
- */
- public class ExException07 {
- public static void main(String[] args){
- try{
- Calculadora calc = new Calculadora();
- System.out.println("Resultado da divisão: "+ calc.calculaMedia(12.0, 0.0));
- }
- catch(DivPorZero e){
- System.out.println(e);
- }
- }
- }
Classe Calculadora:
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package modelo;
- import controle.DivPorZero;
- /**
- *
- * @author Paulo Henrique Cayres
- */
- public class Calculadora {
- public double calculaMedia(double a, double b) throws DivPorZero {
- if (b == 0) {
- throw new DivPorZero();
- }
- return a/b;
- }
- }
Classe DivPorZero:
- package controle;
- /**
- *
- * @author Paulo Henrique Cayres
- */
- public class DivPorZero extends Exception {
- /**
- *
- * @return - Mensagem de erro de divisão por zero.
- */
- @Override
- public String toString() {
- return "Erro de divisão por zero!";
- }
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package visao;
- import controle.ErroStringVasia;
- import modelo.TestaString;
- /**
- *
- * @author paulo983
- */
- public class ExTestaString {
- public static void main(String[] args) {
- TestaString teste = new TestaString();
- try {
- double num = teste.retornaDouble(javax.swing.JOptionPane.showInputDialog("Digite um número"));
- javax.swing.JOptionPane.showMessageDialog(null, "Numero digitado: "+num, "Mensagem", javax.swing.JOptionPane.INFORMATION_MESSAGE);
- } catch (ErroStringVasia ex) {
- javax.swing.JOptionPane.showMessageDialog(null, ex, "Mensagem",javax.swing.JOptionPane.ERROR_MESSAGE);
- }
- catch (NumberFormatException ex){
- javax.swing.JOptionPane.showMessageDialog(null, ex, "Mensagem",javax.swing.JOptionPane.ERROR_MESSAGE);
- }
- }
- }
Classe ErroStringVasia:
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package controle;
- /**
- *
- * @author Paulo Henrique Cayres
- */
- public class ErroStringVasia extends Exception {
- /**
- *
- * @return Mensagem de erro de conversão de tipos
- */
- @Override
- public String toString() {
- return "Impossível converter. Favor informar apenas dígitos na entrada de dados!";
- }
- }
Classe TestaString:
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package modelo;
- import controle.ErroStringVasia;
- /**
- *
- * @author paulo983
- */
- public class TestaString {
- public double retornaDouble(String temp) throws ErroStringVasia {
- if (temp.isEmpty()) throw new ErroStringVasia();
- return Double.parseDouble(temp);
- }
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package visao;
- /**
- *
- * @author Aluno
- */
- public class ExemploExceptionMain {
- public static void main(String[] args){
- System.out.println(args[0]);
- }
- }














 
No comments:
Post a Comment