Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. package com.dorpeled;
  2.  
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5.  
  6. // Main Class
  7. public class Main {
  8.     /***
  9.      * This class uses the 'throws' keyword to make us handle
  10.      * some exceptions if we want to use that function
  11.      * @throws Exception custom exception
  12.      * @throws InputMismatchException insert char instead of int
  13.      * @throws ArithmeticException divide by 0
  14.      */
  15.     static void fun() throws Exception,InputMismatchException, ArithmeticException {
  16.         Scanner scanner = new Scanner(System.in);
  17.         int num1, num2;
  18.         double res;
  19.  
  20.         num1 = scanner.nextInt();
  21.         num2 = scanner.nextInt();
  22.         if(num2 == 0)
  23.             throw new Exception("num2 == 0, cant divide by 0, you should have known better");
  24.         res = num1 / num2;
  25.     }
  26.  
  27.     /***
  28.      * fun has to be surrounded by a try/catch block
  29.      * to satisfy fun()'s exception handling
  30.      * the Exception catch block (the last one)
  31.      * is for our custom exception of type Exception.
  32.      */
  33.     public static void main(String[] args) {
  34.         try {
  35.             fun();
  36.         } catch (InputMismatchException e) {
  37.             System.out.println("Thrown from InputMismatchException");
  38.             System.out.println(e.getMessage());
  39.         } catch (ArithmeticException e) {
  40.             System.out.println("Thrown from ArithmeticException");
  41.             System.out.println(e.getMessage());
  42.         } catch (Exception e) {
  43.             System.out.println("Thrown from Exception");
  44.             System.out.println(e.getMessage());
  45.         }
  46.         int a = 5;
  47.         System.out.println(a);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement