Output of Java Programs | Set 44 (throws keyword)

Prerequisite : throws

1. What will be the output of the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Geeks throws ArithmeticException {
public
    static void main(String[] args) throws ArithmeticException
    {
        System.out.printl(10 / 0);
        System.out.println("Hello Geeks");
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Geeks {
public
    static void main(String[] args) throws Geeks
    {
        Thread.sleep(10000);
        System.out.println("Hello Geeks");
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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");
    }
}

chevron_right


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?


filter_none

edit
close

play_arrow

link
brightness_4
code

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");
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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");
    }
}

chevron_right


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.

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up