Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

prediction failed

Thread view: 
gk - 13 Sep 2006 17:57 GMT
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 ?
Arne Vajhøj - 13 Sep 2006 18:09 GMT
> 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
Arne Vajhøj - 13 Sep 2006 18:09 GMT
>> so, predicted output is :  c  d
>>
[quoted text clipped - 3 lines]
>
> Try this code:

The point is that finally is not a "catch all" but
a "do this no matter what".

Arne
gk - 13 Sep 2006 18:54 GMT
} 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.

> >> so, predicted output is :  c  d
> >>
[quoted text clipped - 8 lines]
>
> Arne
Patricia Shanahan - 13 Sep 2006 20:03 GMT
>                  } finally {
>                         System.out.println("c");
[quoted text clipped - 5 lines]
> 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
Patricia Shanahan - 13 Sep 2006 18:36 GMT
> class Unchecked {
>     public static void main(String[] args) {
[quoted text clipped - 52 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.