Does this finally get executed ?

Discussion in 'Java' started by GIMME, Oct 29, 2004.

  1. GIMME

    GIMME Guest

    Psuedo code ...

    Does sc.close() get executed ?

    public static Employee get(int id ) throws Exception
    {
    try {
    SomeConnection sc = new SomeConnection();
    return new Employee();
    }finally {
    sc.close();
    }
     
    GIMME, Oct 29, 2004
    #1
    1. Advertising

  2. GIMME

    Tony Morris Guest

    "GIMME" <> wrote in message
    news:...
    > Psuedo code ...
    >
    > Does sc.close() get executed ?
    >
    > public static Employee get(int id ) throws Exception
    > {
    > try {
    > SomeConnection sc = new SomeConnection();
    > return new Employee();
    > }finally {
    > sc.close();
    > }


    No. It won't compile.
    Nothing is executed.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Oct 29, 2004
    #2
    1. Advertising

  3. GIMME

    VisionSet Guest

    "GIMME" <> wrote in message
    news:...
    > Psuedo code ...
    >
    > Does sc.close() get executed ?
    >
    > public static Employee get(int id ) throws Exception
    > {
    > try {
    > SomeConnection sc = new SomeConnection();
    > return new Employee();
    > }finally {
    > sc.close();
    > }


    Yes, finally is always executed.
    I think the only exception is calling System.exit(0)

    --
    Mike W
     
    VisionSet, Oct 29, 2004
    #3
  4. GIMME

    VisionSet Guest

    "Tony Morris" <> wrote in message
    news:bnygd.3142$...

    >
    > No. It won't compile.
    > Nothing is executed.
    >


    > Psuedo code ...


    --
    Mike W
     
    VisionSet, Oct 29, 2004
    #4
  5. GIMME

    Tony Morris Guest

    "VisionSet" <> wrote in message
    news:ttygd.180$...
    >
    >
    > "Tony Morris" <> wrote in message
    > news:bnygd.3142$...
    >
    > >
    > > No. It won't compile.
    > > Nothing is executed.
    > >

    >
    > > Psuedo code ...


    It won't compile.
    There is a scope error.
    Nothing is executed.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Oct 29, 2004
    #5
  6. GIMME

    Tony Morris Guest

    > Yes, finally is always executed.
    > I think the only exception is calling System.exit(0)


    There are other exceptions:
    - throwing an exception from within a finally block.
    - explicitly returning from within a finally block.
    Both of these should be avoided as a matter of form.

    Note, however, that the given example will not compile due to a scope
    problem.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Oct 29, 2004
    #6
  7. GIMME

    VisionSet Guest

    "Tony Morris" <> wrote in message
    news:zwygd.3158$...

    > > >
    > > > No. It won't compile.
    > > > Nothing is executed.
    > > >

    > >
    > > > Psuedo code ...

    >
    > It won't compile.
    > There is a scope error.
    > Nothing is executed.


    I think scoping issues may be arguably left out of pseudocode.
    Not that I'm advocating sloppy questions, even though I'm probably guilty of
    them myself a fair bit.

    --
    Mike W
     
    VisionSet, Oct 29, 2004
    #7
  8. GIMME

    Chris Smith Guest

    says...
    > > Yes, finally is always executed.
    > > I think the only exception is calling System.exit(0)

    >
    > There are other exceptions:
    > - throwing an exception from within a finally block.
    > - explicitly returning from within a finally block.
    > Both of these should be avoided as a matter of form.


    Neither of these cases will prevent a finally block from executing.
    They might, however, prevent it from completing normally and therefore
    cause an exception to be ignored; perhaps that's what you're thinking
    of.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Oct 29, 2004
    #8
  9. "Tony Morris" <> writes:

    >> Yes, finally is always executed.
    >> I think the only exception is calling System.exit(0)

    >
    > There are other exceptions:
    > - throwing an exception from within a finally block.
    > - explicitly returning from within a finally block.


    In both cases, the finally block *is* executed. It's just
    that perhaps not all of it is, but that's not much different
    from:
    try {
    ...
    } finally {
    if (false) { ... }
    }

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Oct 29, 2004
    #9
  10. GIMME

    Tony Morris Guest

    I apologise for the ambiguity.
    This is what I meant.

    By "executing a finally block", I meant executing from start to end as is
    often (should be) intended.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Oct 29, 2004
    #10
  11. GIMME

    Tony Morris Guest


    > In both cases, the finally block *is* executed. It's just
    > that perhaps not all of it is, but that's not much different
    > from:
    > try {
    > ...
    > } finally {
    > if (false) { ... }
    > }


    Not quite.
    There is a difference.
    Generally, one is very nasty (returning or throwing from a finally); and one
    isn't (an if construct in a finally block - perfectly legitimate).
    Google appears to have thousands of explanations.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Oct 30, 2004
    #11
  12. GIMME

    Ann Guest

    "GIMME" <> wrote in message
    news:...
    > Psuedo code ...
    >
    > Does sc.close() get executed ?
    >
    > public static Employee get(int id ) throws Exception
    > {
    > try {
    > SomeConnection sc = new SomeConnection();
    > return new Employee();
    > }finally {
    > sc.close();
    > }


    Maybe, if "SomeConnection();"
    has an exception it will
     
    Ann, Oct 30, 2004
    #12
  13. GIMME

    Tony Morris Guest

    > Maybe, if "SomeConnection();"
    > has an exception it will
    >


    For every question asked, there's gotta be the Red Herring Award.
    You win.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Oct 30, 2004
    #13
  14. GIMME

    Brusque Guest

    "Ann" <> wrote in message
    news:foAgd.439881$mD.381037@attbi_s02...
    >
    > Maybe, if "SomeConnection();"
    > has an exception it will
    >


    Liz I'm sure you're trying to be helpful, but wrong answers don't help
    anyone :(
     
    Brusque, Oct 30, 2004
    #14
  15. "VisionSet" <> writes:

    > I think scoping issues may be arguably left out of pseudocode.


    If it's pseudocode it's not Java, and as such we cannot answer because
    try/finally might do something other in that pseudocode language.

    IF the code was rewritten to actually compile, then the try block
    would execute in the example.
     
    Tor Iver Wilhelmsen, Oct 30, 2004
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dannyDog
    Replies:
    56
    Views:
    47,649
    George Cherry
    Jun 15, 2005
  2. Stimp
    Replies:
    2
    Views:
    3,064
    Karl Seguin [MVP]
    Jun 23, 2006
  3. Neroku
    Replies:
    9
    Views:
    1,021
    Chris Uppal
    Feb 7, 2007
  4. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    799
    Alvin Bruney [MVP]
    May 11, 2007
  5. dk
    Replies:
    10
    Views:
    606
    Arne Vajhøj
    Aug 21, 2009
Loading...

Share This Page