What will be the output of the following Java code? [code] class Myexception extends Exception { int detail; Myexception(int

Ngô Thị Vân

New member
What will be the output of the following Java code?
Code:
    class Myexception extends Exception 
    {
	int detail;
        Myexception(int a) 
        {
        detail = a;
	}
	public String toString() 
        {
	    return "detail";
	}
    }
    class Output 
    {
        static void compute (int a) throws Myexception
        {
	     throw new Myexception(a);	 
	}
	public static void main(String args[]) 
        {
            try 
            {
                compute(3);
            }
           catch(DevideByZeroException e)
           {
               System.out.print("Exception");
           } 
        }
    }
A. 3
B. Exception
C. Runtime Error
D. Compilation Error
 
Answer: c
Explanation: Mexception is self defined exception, we are generating Myexception but catching DevideByZeroException which causes error.
Output:
Code:
$ javac Output.javac
 

Tìm kiếm

Back
Top