prediction failed

G

gk

class Unchecked {
public static void main(String[] args) {
try {
method();
} catch (Exception e) {

}
}

static void method() {
try {
wrench();

System.out.println("a");
} catch (ArithmeticException e) {
System.out.println("b");
} finally {
System.out.println("c");
}

System.out.println("d");
}

static void wrench() {
throw new NullPointerException();
}
}






Question:

whats the output ?

i know whats the output ...because i have run it .....but problem is my
prediction and the real output did not match.

see, them method wrench throwing a null pointer exception.


can anybody please tell me the output in steps ?

i dont want to manipulate.

here are things so far........

method() calls wrench()

but wrench() thorws null pointer

this is caught in try-catch .....but there is no matching exception so
it will go to finally ...so "c' would be printed ....and at last d
is there ....so d would be printed.


so, predicted output is : c d

real output: c

question: why d is missing in the real output ?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

gk said:
so, predicted output is : c d

real output: c

question: why d is missing in the real output ?

Try this code:

class Unchecked {
public static void main(String[] args) {
try {
method();
} catch (Exception e) {
System.out.println("e");
}
}

static void method() {
try {
wrench();

System.out.println("a");
} catch (ArithmeticException e) {
System.out.println("b");
} finally {
System.out.println("c");
}

System.out.println("d");
}

static void wrench() {
throw new NullPointerException();
}
}

Arne
 
P

Patricia Shanahan

gk said:
class Unchecked {
public static void main(String[] args) {
try {
method();
} catch (Exception e) {

}
}

static void method() {
try {
wrench();

System.out.println("a");
} catch (ArithmeticException e) {
System.out.println("b");
} finally {
System.out.println("c");
}

System.out.println("d");
}

static void wrench() {
throw new NullPointerException();
}
}






Question:

whats the output ?

i know whats the output ...because i have run it .....but problem is my
prediction and the real output did not match.

see, them method wrench throwing a null pointer exception.


can anybody please tell me the output in steps ?

i dont want to manipulate.

here are things so far........

method() calls wrench()

but wrench() thorws null pointer

this is caught in try-catch .....but there is no matching exception so
it will go to finally ...so "c' would be printed ....and at last d
is there ....so d would be printed.


so, predicted output is : c d

real output: c

question: why d is missing in the real output ?

"If the run-time type of V is not assignable to the parameter of any
catch clause of the try statement, then the finally block is executed.
Then there is a choice:

* If the finally block completes normally, then the try statement
completes abruptly because of a throw of the value V."

[JLS, 14.19.2 Execution of try-catch-finally,
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236653]

This is the case you have. NullPointerException is not assignable to an
ArithmeticException parameter. The finally clause does complete
normally. The whole try-catch-finally statement completes abruptly due
to a throw of a NullPointerException, preventing execution of any
following code in method().

I have yet to see a situation in which a catch of something like
Exception with an empty catch block is a good idea. If the body of main
had just called method, without the try-catch, you would have seen main
terminate due to the NullPointerExceptionn from wrench, and had a much
better chance of working out for yourself what was going on.

Patricia
 
G

gk

} finally {
System.out.println("c");
}

so,it seems that finally does not catch anything ..........it just
executes at last.....but we see there is a null pointer exception which
nobody caught nd so the program got unexpected stop and passes the
control tothe methdo() without printing "d".........is this all right
?



thanks for response.
 
P

Patricia Shanahan

gk said:
} finally {
System.out.println("c");
}

so,it seems that finally does not catch anything ..........it just
executes at last.....but we see there is a null pointer exception which
nobody caught nd so the program got unexpected stop and passes the
control tothe methdo() without printing "d".........is this all right
?

"finally" is called "finally" not "catch" because its job is to be
executed at the end of the try-catch-finally statement, not to catch
anything.

Of course, in the unlikely event that you really did want to go ahead
with the "d" printout after a NullPointerException, you could have
chosen to catch it:

public class UncheckedTest {
public static void main(String[] args) {
method();
}

static void method() {
try {
wrench();
System.out.println("a");
} catch (ArithmeticException e) {
System.out.println("b");
} catch (NullPointerException e) {
System.out.println("NullPointerException caught");
}
finally {
System.out.println("c");
}

System.out.println("d");
}

static void wrench() {
throw new NullPointerException();
}
}

output:

NullPointerException caught
c
d

In the original program, the NullPointerException was caught in the end,
by the try-catch in main. That was the closest try-catch with a catch
clause that could handle it.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top