Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dorpeled;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- // Main Class
- public class Main {
- /***
- * This class uses the 'throws' keyword to make us handle
- * some exceptions if we want to use that function
- * @throws Exception custom exception
- * @throws InputMismatchException insert char instead of int
- * @throws ArithmeticException divide by 0
- */
- static void fun() throws Exception,InputMismatchException, ArithmeticException {
- Scanner scanner = new Scanner(System.in);
- int num1, num2;
- double res;
- num1 = scanner.nextInt();
- num2 = scanner.nextInt();
- if(num2 == 0)
- throw new Exception("num2 == 0, cant divide by 0, you should have known better");
- res = num1 / num2;
- }
- /***
- * fun has to be surrounded by a try/catch block
- * to satisfy fun()'s exception handling
- * the Exception catch block (the last one)
- * is for our custom exception of type Exception.
- */
- public static void main(String[] args) {
- try {
- fun();
- } catch (InputMismatchException e) {
- System.out.println("Thrown from InputMismatchException");
- System.out.println(e.getMessage());
- } catch (ArithmeticException e) {
- System.out.println("Thrown from ArithmeticException");
- System.out.println(e.getMessage());
- } catch (Exception e) {
- System.out.println("Thrown from Exception");
- System.out.println(e.getMessage());
- }
- int a = 5;
- System.out.println(a);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement