File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 Murach's Java Servlets and JSP this week in the Servlets 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: 3419
    
  12
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: 37936
    
  22
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: 37936
    
  22
Originally posted by seetharaman venkatasamy:
Thanks Cambpell ,GOT it


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About try catch confusion
 
Similar Threads
The method must return a result type of int?
Try catch block.
Keyboard.Class/Anyone with brains.pt2
Reg. return from try-catch-finally
final variable try/catch dilemma