Java Exception Handling (Try-catch)

  • + 1 comment

    Here's my code. by using getClass().getName() you handle all exceptions that may occur.

    try

    {

            Scanner sc = new Scanner(System.in);
    
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a/b);
                        sc.close();
        }
        catch (ArithmeticException e)
        {
            System.out.println(e);
        }
        catch (Exception e)
        {
            System.out.println(e.getClass().getName());
        }