Advanced Java Programming - Old Questions

10. Discuss any 5 exception classes in java.

5 marks | Asked in 2070

Given below is a list of the exceptions classes:

  1. ArithmeticException: It is thrown when an exceptional condition has occurred in an arithmetic operation like divide by zero.
  2. ArrayIndexOutOfBoundsException: It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
  3. ClassNotFoundException: This Exception is raised when we try to access a class whose definition is not found.
  4. FileNotFoundException: This Exception is raised when a file is not accessible or does not open.
  5. IOException: It is thrown when an input-output operation failed or interrupted.

Example of Arithmetic Exception:

class ArithmeticException_Demo
{
    public static void main(String args[])
    {
        try {
            int a = 30, b = 0;
            int c = a/b;  // cannot divide by zero
            System.out.println ("Result = " + c);
        }
        catch(ArithmeticException e) {
            System.out.println ("Can't divide a number by 0");
        }
    }
}