Sandeep Roy Sandeep Roy - 1 year ago 59
Java Question

Java Exception Handling understanding issue

I'm not able to understand this program. I expect it to output "Hello World", but instead it prints only the "World". I thought that first the

block would execute, printing "Hello" and " ", then afterward when it encounters a
, it would throw an
. The exception would be caught by
block, then "World" would be printed.

The program is as follows.

import java.util.*;
class exception{
public static void main(String args[])
System.out.println("Hello"+" "+1/0);
catch(ArithmeticException e)

Answer Source

First "Hello"+" "+1/0 will be evaluated. And then passed as an argument to System.out.println(...). That's why an exception is thrown before System.out.println(...) would have been called.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download