Java exception exceptions handling try catch finally

#Java #exception #exceptions #handling #try #catch #finally

41 Comments

  1. import java.util.InputMismatchException;

    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {

    // exception = an event that occurs during the execution of a program that,

    // disrupts the normal flow of instructions

    Scanner scanner = new Scanner(System.in);

    try {

    System.out.println("Enter a whole number to divide: ");

    int x = scanner.nextInt();

    System.out.println("Enter a whole number to divide by: ");

    int y = scanner.nextInt();

    int z = x/y;

    System.out.println("result: " + z);

    }

    catch(ArithmeticException e) {

    System.out.println("You can't divide by zero! IDIOT!");

    }

    catch(InputMismatchException e) {

    System.out.println("PLEASE ENTER A NUMBER OMFG!!!");

    }

    catch(Exception e) {

    System.out.println("Something went wrong");

    }

    finally {

    scanner.close();

    }

    }

    }

  2. 8 minutes… I'm in a hour and a half lesson just on exception handling. And you just explained like 88% of it in 8 minutes.

  3. Hi, I want to ask when I want to use the input outside the try block, but I put it inside the try block. How can I do that? Because the input is not visible outside the try block.

Write A Comment