Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- /**
- * Classe para gerar objetos do tipo Divisão que vão demonstrar os tratamentos
- * try .. catch .. throws.
- *
- * @author
- * @since Classe criada em 20/11/2017
- */
- public class Divisao {
- public static void main(String[] args) {
- int a, b, div = 0;
- String num1, num2;
- num1 = JOptionPane.showInputDialog("Digite o primeiro número: ");
- num2 = JOptionPane.showInputDialog("Digite o segundo número: ");
- try {
- a = Integer.parseInt(num1);
- b = Integer.parseInt(num2);
- div = divisao(a, b);
- System.out.println("A divisão é: " + div);
- } catch (ArithmeticException e) {
- System.out.println("Não é possivel dividir por zero");
- } catch (NumberFormatException e) {
- System.out.println("Somente números inteiros");
- } catch (Excecoes e) {
- System.out.println("Exceção tipo: " + e.toString());
- } catch (Exception e) {
- System.out.println("Erro: " + e.toString());
- }
- }//fim do main
- public static int divisao(int a, int b) throws Excecoes {
- if (b > a) {
- throw new Excecoes();
- }
- if (a + b > 200) {
- throw new Excecoes(1);
- }
- if (a > 150) {
- throw new Excecoes("x");
- }
- if (b < 0) {
- throw new Excecoes(true);
- }
- return a / b;
- }
- }//fim da classe
Add Comment
Please, Sign In to add comment