Menu
Topics Index
...
`

try catch


If you need explanation Read this topic

If you need Answer Take test on this topic

try catch
In which order will the lines appear in the output of this program? Or does it not compile?
public class SecondException
{
    public static void main(String args[])
    {
        try
        {
            int i = 21 / 0;
            System.out.println(i);
        }
        catch (NullPointerException e)
        {
            System.out.println(e);
        }
        
        System.out.println("Rest of the code......");
    }
}

A. java.lang.ArithmeticException: / by zero
B. java.lang.ArithmeticException: / by zero
Rest of the code......
C. 0
Rest of the code......

D. Compilation Error for some other reason
Topic: Try Catch Block In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans is A,,,,21/0 is infinite,,,there is no  data type to represent this kind of infinte,,,,error occurs and makes the program to end abruptly,,,the garbage collection also becomes huge,,,to avaoid
these simply handle them with prtinf statement  whenever such errors occur,,,,
try
{
exception-if it is arithmetic
}
catch
{

pritnf what it is,,,
}

handling these kind of exceptions is called exceptional handling

Posted by Sai Veerendra    2014-12-11 16:59:28


ans a

Exception handlers can be nested within one another. A try, catch or a finally block can in turn contains another set of try catch finally sequence. In such a scenario, when a particular catch block is unable to handle an Exception, this exception is rethrown. This exception would be handled by the outer set of try catch handler . Exception handling is a mechanism that allows Java programs to handle various exceptional conditions,
such as semantic violations of the language and program-defined errors, in a robust way. When an
exceptional condition occurs, an exception is thrown. If the Java virtual machine or run-time environment
detects a semantic violation, the virtual machine or run-time environment implicitly throws an exception. if an exception
is thrown, control is transferred from the current point of execution to an appropriate catch clause of an
enclosing try statement. The catch clause is called an exception handler because it handles the
exception by taking whatever actions are necessary to recover from it. Putting a block in a try statement indicates that any exceptions or other abnormal.

coming to this program int i=21/0 thrown arithmetic exception:/by zero -runtime error.

this is arithmethic exception so this is not handled with given catch ,which has nullpointerException handler.

Posted by Maheshwari Natarajan    2014-12-11 17:45:34


Answer is A.

These are my findings on this problem.

1. Since "Divide by Zero" is an exception, it abrupts the program execution and shows "ArithmeticException: / by zero" at console.

2. In the catch block of the above program, if there is a general "Exception" instead of a specific "NullPointerException", then the control enters that block and executes statements over there.

3. Instead of "NullPointerException" in catch block, if "ArithmeticException" is there, then also the control would have entered into it and execute code in that block.

4. In any of the above two cases if control enters the catch block and exits it without any further issues then it executes rest of the code present after the catch block.

Posted by Raja Sekhar    2014-12-11 19:07:59


a

Posted by Shubham Bansal    2014-12-13 09:30:49


This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Raja Sekhar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-14 01:19:41


© meritcampus 2019

All Rights Reserved.

Open In App