Open In App
Related Articles

Output of Java Programs | Set 44 (throws keyword)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
Prerequisite : throws 1. What will be the output of the following program?
class Geeks throws ArithmeticException {
public
    static void main(String[] args) throws ArithmeticException
    {
        System.out.printl(10 / 0);
        System.out.println("Hello Geeks");
    }
}

                    
Options: 1. Hello Geeks 2. No Output 3. Run-time Exception 4. Compile time error Output:
The answer is option (4)
Explanation : In the above program, we are throwing ArithmeticException from the class. But we have to take care of the convention when we are using throws keyword that we can use throws keyword for methods and constructors but not for classes. 2. What will be the output of the following program?
class Geeks {
public
    static void main(String[] args) throws Geeks
    {
        Thread.sleep(10000);
        System.out.println("Hello Geeks");
    }
}

                    
Options: 1. Hello Geeks 2. compile time error 3. No Output 4. Run-time exception Output:
The answer is option (2)
Explanation : We can use throws keyword only for throwable types. If we are trying to use for normal java classes then we will get compile time error saying incompatible types. 3. What will be the output of the following program?
class Test {
    void m1() throws ArithmeticException
    {
        throw new ArithmeticException("Calculation error");
    }
    void m2() throws ArithmeticException
    {
        m1();
    }
    void m3()
    {
        try {
            m2();
        }
        catch (ArithmeticException e) {
            System.out.println("ArithmeticException");
        }
    }
public
    static void main(String args[])
    {
        Test t = new Test();
        t.m3();
        System.out.println("Handled by Geeks");
    }
}

                    
Options: 1. Calculation error 2. ArithmeticException 3. Handled by Geeks 4. ArithmeticException Handled by Geeks Output:
The answer is option (4)
Explanation : As you can see that we have an exception occurred in m1() method which has been handled in the chain-calling method m3(). 4. What will be the output of the following program?
class Test {
    void Div() throws ArithmeticException
    {
        int a = 25, b = 0, rs;
        rs = a / b;
        System.out.print("\n\tThe result is : " + rs);
    }
public
    static void main(String[] args)
    {
        Test t = new Test();
        try {
            t.Div();
        }
        catch (ArithmeticException e) {
            System.out.print("\n\tError : " + e.getMessage());
        }
        System.out.print("\n\tBYE GEEKS");
    }
}

                    
Options: 1. BYE GEEKS 2. Compile time error 3. Error: / by zero 4. Error : / by zero BYE GEEKS Output:
The answer is option (4)
Explanation : In the above program, Div() method is throwing an ArithmeticException, therefore, we have written the calling statement of Div() method in try-catch block. 5. What will be the output of the following program?
class Test {
public
    static void main(String[] args)
    {
        fun();
    }
public
    static void fun()
    {
        moreFun();
    }
public
    static void moreFun() throws InterruptedException
    {
        Thread.sleep(10000);
        System.out.println("GEEKS");
    }
}

                    
Options: 1. GEEKS 2. No Output 3. Compile time error 4. Run-time Exception Output:
The answer is option (3)
Explanation : In the above program, we are throwing InterruptedException from a method where exception may be rise. But we will get compile time error saying error: unreported exception InterruptedException; must be caught or declared to be thrown because moreFun() method is called by fun() method and fun() method is called by main() method. Thats why we have to use throws keyword for every caller.

Last Updated : 29 Sep, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads