Re: variable scope with finally

Discussion in 'Java' started by J.W, Jul 9, 2003.

  1. J.W

    J.W Guest

    I haven't much to add that hasn't already been said...
    but one helpful tip to remember is that whenever you create a variable, if
    it is not static it will only exist between the opening and closing braces
    that enclose it (and of course it's accessible in nested brackets).

    The good thing about java is that it's a tad easier to see where a block
    starts, since java will not let you create a block simply by using opening
    and closing brackets (unlike C or C++), you have to use a statement like if,
    while, for, try, catch, finally etc etc...


    "Rui Pacheco" <> wrote in message
    news:b54pl3$34h$...
    > I have a simple app:
    >
    >
    > public myclass{
    >
    > public blah main( String[] args ){
    >
    > try{
    > MyCodeThatThrowsAnException();
    >
    > SomeObject myInstance = new SomeObject();
    > myInstance.itsMethod();
    >
    > }catch( MyException ){
    > System.out.println("Wow, barely caught that exception!");
    > }
    >
    > finally{
    > //Final clean up here
    > }
    > }
    > }
    >
    >
    > Well, this is more or less like my app. My problem is, variables that

    I
    > have initiated inside the try{} block don't seem to exist in the finally.
    > This means that if my objects have methods that should be used in the
    > cleanup, they can't be accessed in the finally.
    > Am I right or am I missing something? If I'm right, how can I reach

    the
    > methods of the objects that were initiated inside the try block?
    >
    > Thank you very much
    > Rui Pacheco
    >
    >
     
    J.W, Jul 9, 2003
    #1
    1. Advertising

  2. "J.W" <> writes:
    > The good thing about java is that it's a tad easier to see where a block
    > starts, since java will not let you create a block simply by using opening
    > and closing brackets (unlike C or C++), you have to use a statement like if,
    > while, for, try, catch, finally etc etc...


    Your claim is simply wrong.

    /Thomas
     
    Thomas Weidenfeller, Jul 9, 2003
    #2
    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. Paul Opal
    Replies:
    12
    Views:
    963
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    676
    Patricia Shanahan
    Sep 13, 2005
  3. David Lozzi

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

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    801
    Alvin Bruney [MVP]
    May 11, 2007
  4. David Filmer
    Replies:
    19
    Views:
    250
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    202
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page