Exceptions


  1. What is the output of this program?
    class Myexception extends Exception 
    {
    int Execution;
    Myexception(int p)
    {
    Execution = p;
    }
    public String toString()
    {
    return "Execution";
    }
    }

    public class Result
    {
    static void compute (int p) throws Myexception
    {
    throw new Myexception(p);
    }
    public static void main(String args[])
    {
    try
    {
    compute(6);
    }
    catch(DevideByZeroException e)
    {
    System.out.print("Catch block Executed...");
    }
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Mexception is self defined exception, we are generating Myexception but catching DevideByZeroException which causes error.

    error: cannot find symbol
    catch(DevideByZeroException e)
    ^
    symbol: class DevideByZeroException
    location: class Result
    1 error


  1. What is the output of this program?
    public class exceptionHandling_Example
    {
    public static void main(String args[])
    {
    try
    {
    throw new NullPointerException ("Hello");
    System.out.print("First");
    }
    catch(ArithmeticException e)
    {
    System.out.print("Second");
    }
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.

    error: unreachable statement
    System.out.print("First");



  1. What is the output of this program?
    class Newexception extends Exception 
    {
    int Execution;
    Newexception(int p)
    {
    Execution = p;
    }
    public String toString()
    {
    return "Execution";
    }
    }

    public class Output
    {
    static void compute (int p) throws Newexception
    {
    throw new Newexception(p);
    }
    public static void main(String args[])
    {
    try
    {
    compute(13);
    }
    catch(Exception e)
    {
    System.out.print("Newexception working proper...");
    }
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Newexception is self defined exception.


  1. Which of these operator is used to generate an instance of an exception than can be thrown by using throw?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    new is used to create an instance of an exception. All of java’s built in run-time exceptions have two constructors: one with no parameters and one that takes a string parameter.



  1. Which of these is a super class of all exceptional type classes?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    All the exception types are subclasses of the built in class Throwable.