Exceptions


  1. What is the output of this program?
    public class exceptionHandling_Example
    {
    public static void main(String args[])
    {
    try
    {
    int n = args.length;
    int n1 = 9 / n;
    System.out.print(n);
    try
    {
    if (n == 1)
    n = n / n - n;
    if (n == 2)
    {
    int s = {1};
    s[8] = 9;
    }
    }
    catch (ArrayIndexOutOfBoundException e)
    {
    System.out.println("Hello");
    }
    catch (ArithmeticException e)
    {
    System.out.println("Java");
    }
    }
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    Compilation Error

    error: 'try' without 'catch', 'finally' or resource declarations
    try
    ^
    1 error


  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. 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 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?
    class Myexception extends Exception 
    {
    int Execution;
    Myexception(int n)
    {
    Execution = n;
    }
    public String toString()
    {
    return "Execution";
    }
    }

    public class UserDefine_Exception
    {
    static void compute (int n) throws Myexception
    {
    throw new Myexception(n);
    }
    public static void main(String args[])
    {
    try
    {
    compute(5);
    }
    catch(Myexception e)
    {
    System.out.print("User define Exception executed...");
    }
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Myexception is self defined exception.