Java find output programs (Exception Handling) | set 2

Find the output of Java programs | Exception Handling | Set 2: Enhance the knowledge of Java Exception Handling concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 04, 2021

Question 1:

public class ExpEx
{
     public static void main(String []args)
     {
        int Arr[] = { 5, 4, 3, 2, 1 };
        int loop=0;

        try
        {
            for(loop=0;loop<=5;loop++)
                System.out.println(Arr[loop]);
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
     }
}

Output:

5
4
3
2
1
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5

Explanation:

In the above program, we created an ExpEx class that contains the main() method. Here, we created an integer array with 5 elements, and we also created an integer variable loop initialized with 0. Then we used the for loop to print array elements. But here we accessed array element at index 5, which is not exist in the array that's why the ArrayIndexOutOfBoundException exception generated and caught by catch block and print appropriate exception message on the console screen.

Question 2:

public class ExpEx
{
     public static void main(String []args)
     {
        int Arr[] = { 5, 4, 3, 2, 1 };
        int loop=0;
        int val = 0;

        try
        {
            for(loop=0;loop<5;loop++)
                System.out.println(Arr[loop]);
        }
        catch (Exception e)
        {
            System.out.println(e.message());
        }
     }
}

Output:

/ExpEx.java:16: error: cannot find symbol
            System.out.println(e.message());
                                ^
  symbol:   method message()
  location: variable e of type Exception
1 error

Explanation:

The above program will generate syntax error because Exception class does not contain method message() that's why syntax error gets generated.

The Exception class contains the getMessage() method to get exception messages.

Question 3:

public class ExpEx {
  public static void main(String[] args) {
    try {
      ArithmeticException OB = new ArithmeticException("My Exception");

      through OB;
    }
    catch(Exception e) {
      System.out.println(e.getMessage());
    }
  }
}

Output:

/ExpEx.java:6: error: cannot find symbol
      through OB;
      ^
  symbol:   class through
  location: class ExpEx
/ExpEx.java:6: error: variable OB is already defined in method main(String[])
      through OB;
              ^
2 errors

Explanation:

The above program will generate a syntax error because through is not any built-in keyword in Java. We need to use the throw keyword to throw an exception explicitly.

Question 4:

public class ExpEx {
  public static void main(String[] args) {
    try {
      ArithmeticException OB = new ArithmeticException("My Exception");

      throw OB;
    }
    catch(Exception e) {
      System.out.println(e.getMessage());
    }
  }
}

Output:

My Exception

Explanation:

In the above program, we created an ExpEx class that contains the main() method. Here, we created the object OB of ArithmeticException class then throw the object OB that will be caught by the catch block and then print the message "My Exception" using getMessage() method of Exception class on the console screen.

Question 5:

public class ExpEx {
  public static void main(String[] args) {
    try {
      Exception OB = new Exception();

      OB.setMessage("User define exception");
      throw OB;
    }
    catch(Exception e) {
      System.out.println(e.getMessage());
    }
  }
}

Output:

/ExpEx.java:6: error: cannot find symbol
      OB.setMessage("User define exception");
        ^
  symbol:   method setMessage(String)
  location: variable OB of type Exception
1 error

Explanation:

The above program will generate a syntax error because of the below statement,

OB.setMessage("User define exception");

In the above statement we used setMessage() method but Exception class does not contain setMessage(), that's why syntax error gets generated.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.