File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About try catch confusion

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cambpell ,GOT it
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by seetharaman venkatasamy:
Thanks Cambpell ,GOT it


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic