aspose file tools*
The moose likes Beginning Java and the fly likes About  try catch confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About  try catch confusion" Watch "About  try catch confusion" New topic
Author

About try catch confusion

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi, please see the below code


i feel bit difficult to understand return statement with try and catch.

also i am geting the value of method=8

i mean System.out.println(method());// it gives 8 and How?


please anyone explain in detail
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
Originally posted by seetharaman venkatasamy:
i mean System.out.println(method());// it gives 8 and How?


What did you expect it to print ? Your method has a line that says return 8, no exception is thrown and there is nothing in the finally block, so it returns 8.


Joanne
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Neal ... i GOT it ...

one more thing ... in below code...why compilar says that i has not been initialized!


[ October 15, 2008: Message edited by: seetharaman venkatasamy ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
The values of local variables live on the stack; unless a value is specifically inserted by every path through the method, it is possible that you could return whatever was there on the stack beforehand. So the compiler sees that the "try" might not necessarily set a value to i (even if you don't write an actual Exception) so it insists you set a value to i before the "try."

You may also get "unreachable code" problems with that method.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Cambpell ,GOT it
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Originally posted by seetharaman venkatasamy:
Thanks Cambpell ,GOT it


 
wood burning stoves
 
subject: About try catch confusion