Java Exception Handling (Try-catch)

  • + 0 comments

    Thanks. I didn't know I can pass objects to try block.

    4th test case got failed tho, so I fixed it a lil bit.

    public static void main(String[] args)
    {
        try(Scanner in = new Scanner(System.in))
        {
            System.out.println(in.nextInt()/in.nextInt());
        } catch(InputMismatchException ex)
        {
            System.out.println(ex.getClass().getName());
        }
        catch(ArithmeticException ae)
        {
            System.out.println(ae);
        }
    }