Guest User

Untitled

a guest
Nov 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. /**
  4. * Classe para gerar objetos do tipo Divisão que vão demonstrar os tratamentos
  5. * try .. catch .. throws.
  6. *
  7. * @author
  8. * @since Classe criada em 20/11/2017
  9. */
  10. public class Divisao {
  11.  
  12. public static void main(String[] args) {
  13. int a, b, div = 0;
  14. String num1, num2;
  15.  
  16. num1 = JOptionPane.showInputDialog("Digite o primeiro número: ");
  17. num2 = JOptionPane.showInputDialog("Digite o segundo número: ");
  18.  
  19. try {
  20. a = Integer.parseInt(num1);
  21. b = Integer.parseInt(num2);
  22. div = divisao(a, b);
  23. System.out.println("A divisão é: " + div);
  24. } catch (ArithmeticException e) {
  25. System.out.println("Não é possivel dividir por zero");
  26. } catch (NumberFormatException e) {
  27. System.out.println("Somente números inteiros");
  28. } catch (Excecoes e) {
  29. System.out.println("Exceção tipo: " + e.toString());
  30. } catch (Exception e) {
  31. System.out.println("Erro: " + e.toString());
  32. }
  33.  
  34. }//fim do main
  35.  
  36. public static int divisao(int a, int b) throws Excecoes {
  37. if (b > a) {
  38. throw new Excecoes();
  39. }
  40.  
  41. if (a + b > 200) {
  42. throw new Excecoes(1);
  43. }
  44.  
  45. if (a > 150) {
  46. throw new Excecoes("x");
  47. }
  48.  
  49. if (b < 0) {
  50. throw new Excecoes(true);
  51. }
  52.  
  53. return a / b;
  54. }
  55.  
  56. }//fim da classe
Add Comment
Please, Sign In to add comment