• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Local variable initialization question

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can some somebody explain why the compiler complains about value ex not being initialized only inside the finally block and not inside the catch block ?
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't see a compiler error inside of a catch block because the compiler would only care to see if everything is syntactically correct inside of the catch block.
The reason (and this is my guess) is that, you might have called some method in your try block, and might have instantiated ex in that method right?
OR, you might not even enter the catch block during runtime if you don't throw an exception, right?
But the finally block does get executed, no matter what (be it there is an exception or not).
That's what I think, and I'm sure I will be corrected if I'm wrong.

Thanks,
-Vijay
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can some somebody explain why the compiler complains about value ex not being initialized only inside the finally block and not inside the catch block ?


ex is local variable ,you should init it before you using .

maybe you can do this

and the program complie well
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested it using the compiler on the command line and it complained about both assignments.

My guess is that you may be using an IDE and that compiler may have complained about the finally first and the IDE stopped the compiler at that point.

Or it's possible that the compiler did complain but the IDE might not have displayed both errors.
 
Liviu Carausu
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have learned that in such cases I must use the standard compiler first
and trust the java language specification.
It looks like the Eclipse compiler makes some strange and dangerous optimizations. If I replace the comments from the code with "throw new Exception()", then the compiler complains also about the catch block.
Thanks for your help !
Liviu
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will execute the finally block in any case. In this when it goes to initilalise the exception object e2 with ex, it will give error as ex is not initialised.
Correct me if wrong.
Thanks and Regards,
Ani.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Liviu Carausu
i agree with Vijay Gade.
According my knowledge the answer given by Vijay is 100% correct.

At compilation time the catch block is unreachable.
where as the finally block will be executed in all situations except some exceptional cases.

correct me if i am wrong

KiranKumarAlapati
SCJP 1.4
 
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to initialize all local variables. ex is a local variable, so set it to null.
 
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic