├── .idea ├── .gitignore ├── description.html ├── encodings.xml ├── misc.xml ├── modules.xml ├── project-template.xml ├── runConfigurations.xml ├── uiDesigner.xml └── vcs.xml ├── README.md ├── exceptions-java.iml ├── out └── production │ └── exceptions-java │ ├── br │ └── com │ │ └── dio │ │ └── exceptions │ │ ├── CheckedException.class │ │ ├── DivisaoNaoExataException.class │ │ ├── ExceptionCustomizada_1.class │ │ ├── ExceptionCustomizada_2.class │ │ ├── ImpossivelAberturaDeArquivoException.class │ │ └── UncheckedException.class │ └── test │ ├── CheckedException.class │ ├── ExceptionCustomizada.class │ ├── ExceptionCustomizada_2.class │ ├── Exemplo_3.class │ ├── NaoFoiPossivelAbrirOArquivoException.class │ └── UncheckedException.class ├── romances-blake-crouch.txt └── src └── br └── com └── dio └── exceptions ├── CheckedException.java ├── DivisaoNaoExataException.java ├── ExceptionCustomizada_1.java ├── ExceptionCustomizada_2.java └── UncheckedException.java /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/description.html: -------------------------------------------------------------------------------- 1 | Simple Java application that includes a class with main() method -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/project-template.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | TRATAMENTO DE EXCEÇÕES EM JAVA

3 | 4 |

Curso que apresenta conceitos introdutórios sobre como podemos identificar e tratar Exceptions na linguagem Java. 5 | Curso este ministrado por mim em nome da DIGITAL INNOVATION ONE 🧡💛 e disponibilizado de forma gratuita para a comunidade dos desenvolvedores Java. 6 | 7 |

8 | 🛑 Pré-requistos 9 |

10 | 11 | - [x] Java JDK 8 ou superior 12 | - [x] IDE para desenvolvimento Java 13 | - [x] Conhecimento BÁSICO em OOP 14 | - [x] Estar disposto a aprender 15 | 16 |

📚 Ementa

17 |
    18 |
  1. Visão Geral
  2. 19 |
  3. Unchecked Exception
  4. 20 |
  5. Checked Exception
  6. 21 |
  7. Exception Personalizada
  8. 22 |
23 | 24 |

Visão Geral

25 | 35 | 36 |

Error:

Usado pela JVM que serve para indicar se existe algum problema de recurso do programa, tornando a execução impossível de continuar. 37 |

Unchecked (Runtime):

Exceptions que PODEM ser evitados se forem tratados e analisados pelo desenvolvedor. 38 |

Checked Exception:

Exceptions que DEVEM ser evitados etratados pelo desenvolvedor para o programa funcionar. 39 | 40 |
41 | amoTu 42 |

Hierarquia Exceptions

43 |
44 | 45 |

Palavras Reservadas:

46 | 51 | 52 |
53 |

🔸 Error

54 | 58 | 59 |

🔸 Unchecked Exception

60 | 66 | 67 |

🔸 Checked Exception

68 | 76 | 77 |

🔸 Exception Personalizada

78 | 85 | 86 |

🔸 Blocos try/catch/finally

87 | 93 | 101 | 107 | 108 |

🔸 Cláusulas throws e throw

109 | 115 | 125 | 126 |

🔗 Referências

127 | 137 | 138 |

🤝 Contribuindo

139 | 140 | Este repositório foi criado para fins de estudo, então contribua com ele.
141 | Se te ajudei de alguma forma, ficarei feliz em saber. E caso você conheça alguém que se identidique com o conteúdo, não deixe de compatilhar. 142 | 143 | Se possível: 144 | 145 | ⭐️ Star o projeto 146 | 147 | 🐛 Encontrar e relatar issues 148 | 149 | 150 | ------------ 151 | 152 | Disponibilizado com ♥ por [cami-la](https://www.linkedin.com/in/cami-la/ "cami-la"). 153 | -------------------------------------------------------------------------------- /exceptions-java.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /out/production/exceptions-java/br/com/dio/exceptions/CheckedException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/br/com/dio/exceptions/CheckedException.class -------------------------------------------------------------------------------- /out/production/exceptions-java/br/com/dio/exceptions/DivisaoNaoExataException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/br/com/dio/exceptions/DivisaoNaoExataException.class -------------------------------------------------------------------------------- /out/production/exceptions-java/br/com/dio/exceptions/ExceptionCustomizada_1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/br/com/dio/exceptions/ExceptionCustomizada_1.class -------------------------------------------------------------------------------- /out/production/exceptions-java/br/com/dio/exceptions/ExceptionCustomizada_2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/br/com/dio/exceptions/ExceptionCustomizada_2.class -------------------------------------------------------------------------------- /out/production/exceptions-java/br/com/dio/exceptions/ImpossivelAberturaDeArquivoException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/br/com/dio/exceptions/ImpossivelAberturaDeArquivoException.class -------------------------------------------------------------------------------- /out/production/exceptions-java/br/com/dio/exceptions/UncheckedException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/br/com/dio/exceptions/UncheckedException.class -------------------------------------------------------------------------------- /out/production/exceptions-java/test/CheckedException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/test/CheckedException.class -------------------------------------------------------------------------------- /out/production/exceptions-java/test/ExceptionCustomizada.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/test/ExceptionCustomizada.class -------------------------------------------------------------------------------- /out/production/exceptions-java/test/ExceptionCustomizada_2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/test/ExceptionCustomizada_2.class -------------------------------------------------------------------------------- /out/production/exceptions-java/test/Exemplo_3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/test/Exemplo_3.class -------------------------------------------------------------------------------- /out/production/exceptions-java/test/NaoFoiPossivelAbrirOArquivoException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/test/NaoFoiPossivelAbrirOArquivoException.class -------------------------------------------------------------------------------- /out/production/exceptions-java/test/UncheckedException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cami-la/exceptions-java/a108eca3f716e16186de017db88ed93fa886655a/out/production/exceptions-java/test/UncheckedException.class -------------------------------------------------------------------------------- /romances-blake-crouch.txt: -------------------------------------------------------------------------------- 1 | Romances Blake Crouch 2 | 3 | Abandon (July 7, 2009) 4 | Famous (April 15, 2010) 5 | Snowbound (June 22, 2010) 6 | Run (February 24, 2011) 7 | Eerie (with Jordan Crouch (June 7, 2012) 8 | Dark Matter (July 26, 2016) 9 | Good Behavior (November 15, 2016) 10 | Recursion (June 11, 2019) -------------------------------------------------------------------------------- /src/br/com/dio/exceptions/CheckedException.java: -------------------------------------------------------------------------------- 1 | package br.com.dio.exceptions; 2 | 3 | import javax.swing.*; 4 | import java.io.*; 5 | 6 | //Imprimir um arquivo no console. 7 | public class CheckedException { 8 | public static void main(String[] args) { 9 | String nomeDoArquivo = "romance-blake-crouch.txt"; 10 | 11 | try { 12 | imprimirArquivoNoConsole(nomeDoArquivo); 13 | } catch (FileNotFoundException e) { 14 | JOptionPane.showMessageDialog(null, 15 | "Revise o nome do arquivo que você deseja imprimir! " + e.getCause()); 16 | e.printStackTrace(); 17 | } catch (IOException e){ 18 | //e.printStackTrace(); 19 | JOptionPane.showMessageDialog(null, 20 | "Ocorreu um erro inesperado! Entre em contato com o suporte! " + e.getCause()); 21 | } finally { 22 | System.out.println("Chegou no finally!"); 23 | } 24 | 25 | System.out.println("Apesar da exception ou não, o programa continua..."); 26 | } 27 | 28 | public static void imprimirArquivoNoConsole(String nomeDoArquivo) throws IOException { 29 | File file = new File(nomeDoArquivo); 30 | 31 | BufferedReader br = new BufferedReader(new FileReader(file.getName())); 32 | String line = br.readLine(); 33 | 34 | BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); 35 | 36 | do{ 37 | bw.write(line); 38 | bw.newLine(); 39 | line=br.readLine(); 40 | } while(line != null); 41 | bw.flush(); 42 | br.close(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/br/com/dio/exceptions/DivisaoNaoExataException.java: -------------------------------------------------------------------------------- 1 | package br.com.dio.exceptions; 2 | 3 | public class DivisaoNaoExataException extends Exception{ 4 | 5 | private int numerado; 6 | private int denominador; 7 | 8 | public DivisaoNaoExataException(String message, int numerado, int denominador) { 9 | super(message); 10 | this.numerado = numerado; 11 | this.denominador = denominador; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/br/com/dio/exceptions/ExceptionCustomizada_1.java: -------------------------------------------------------------------------------- 1 | package br.com.dio.exceptions; 2 | 3 | import javax.swing.*; 4 | import java.io.*; 5 | 6 | public class ExceptionCustomizada_1 { 7 | public static void main(String[] args) { 8 | String nomeDoArquivo = JOptionPane.showInputDialog("Nome do arquivo a ser exibido: "); 9 | 10 | imprimirArquivoNoConsole(nomeDoArquivo); 11 | System.out.println("\nCom exception ou não, o programa continua..."); 12 | } 13 | 14 | public static void imprimirArquivoNoConsole(String nomeDoArquivo) { 15 | 16 | try { 17 | BufferedReader br = lerArquivo(nomeDoArquivo); 18 | String line = br.readLine(); 19 | BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); 20 | do { 21 | bw.write(line); 22 | bw.newLine(); 23 | line = br.readLine(); 24 | } while (line != null); 25 | bw.flush(); 26 | br.close(); 27 | } catch (ImpossivelAberturaDeArquivoException e) { 28 | JOptionPane.showMessageDialog(null, 29 | e.getMessage()); 30 | //e.printStackTrace(); 31 | } catch (IOException ex) { 32 | JOptionPane.showMessageDialog(null, 33 | "Ocorreu um erro não esperado, por favor, fale com o suporte." + ex.getMessage()); 34 | ex.printStackTrace(); 35 | } 36 | } 37 | 38 | public static BufferedReader lerArquivo(String nomeDoArquivo) throws ImpossivelAberturaDeArquivoException { 39 | 40 | File file = new File(nomeDoArquivo); 41 | try { 42 | return new BufferedReader(new FileReader(nomeDoArquivo)); 43 | } catch (FileNotFoundException e) { 44 | throw new ImpossivelAberturaDeArquivoException(file.getName(), file.getPath()); 45 | } 46 | } 47 | } 48 | 49 | class ImpossivelAberturaDeArquivoException extends Exception { 50 | 51 | private String nomeDoArquivo; 52 | private String diretorio; 53 | 54 | public ImpossivelAberturaDeArquivoException(String nomeDoArquivo, String diretorio) { 55 | super("O arquivo " + nomeDoArquivo + " não foi encontrado no diretório " + diretorio); 56 | this.nomeDoArquivo = nomeDoArquivo; 57 | this.diretorio = diretorio; 58 | } 59 | 60 | /*@Override 61 | public String toString() { 62 | return "ImpossivelAberturaDeArquivoException{" + 63 | "nomeDoArquivo='" + nomeDoArquivo + '\'' + 64 | ", diretorio='" + diretorio + '\'' + 65 | '}'; 66 | }*/ 67 | 68 | } 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/br/com/dio/exceptions/ExceptionCustomizada_2.java: -------------------------------------------------------------------------------- 1 | package br.com.dio.exceptions; 2 | 3 | import javax.swing.*; 4 | 5 | public class ExceptionCustomizada_2 { 6 | public static void main(String[] args){ 7 | int[] numerador = {4, 5, 8, 10}; 8 | int[] denominador = {2, 4, 0, 2, 8}; 9 | 10 | for (int i = 0; i < denominador.length; i++) { 11 | try { 12 | if(numerador[i] %2 != 0) 13 | throw new DivisaoNaoExataException("Divisão não exata!", numerador[i], denominador[i]); 14 | 15 | int resultado = numerador[i] / denominador[i]; 16 | System.out.println(resultado); 17 | } catch (DivisaoNaoExataException | ArithmeticException | ArrayIndexOutOfBoundsException e) { 18 | e.printStackTrace(); 19 | JOptionPane.showMessageDialog(null, e.getMessage()); 20 | } 21 | } 22 | 23 | System.out.println("O programa continua..."); 24 | } 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/br/com/dio/exceptions/UncheckedException.java: -------------------------------------------------------------------------------- 1 | package br.com.dio.exceptions; 2 | 3 | import javax.swing.*; 4 | 5 | //Fazer a divisão de 2 valores inteiros 6 | public class UncheckedException { 7 | public static void main(String[] args) { 8 | 9 | boolean continueLooping = true; 10 | do { 11 | String a = JOptionPane.showInputDialog("Numerador: "); 12 | String b = JOptionPane.showInputDialog("Denominador: "); 13 | 14 | try{ 15 | int resultado = dividir(Integer.parseInt(a), Integer.parseInt(b)); 16 | System.out.println("Resultado: " + resultado); 17 | continueLooping = false; 18 | } catch (NumberFormatException e) { 19 | e.printStackTrace(); 20 | JOptionPane.showMessageDialog(null, "Entrada inválida, informe um número inteiro! " + e.getMessage()); 21 | } catch (ArithmeticException e) { 22 | e.printStackTrace(); 23 | JOptionPane.showMessageDialog(null, "Impossível dividir um número por 0."); 24 | } 25 | finally { 26 | System.out.println("Chegou no finally!"); 27 | } 28 | } while(continueLooping); 29 | 30 | 31 | System.out.println("O código continua..."); 32 | } 33 | 34 | public static int dividir(int a, int b) { 35 | return a / b; 36 | } 37 | } 38 | --------------------------------------------------------------------------------