| 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: 3011
|
|
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: 32675
|
|
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: 32675
|
|
Originally posted by seetharaman venkatasamy: Thanks Cambpell ,GOT it
|
 |
 |
|
|
subject: About try catch confusion
|
|
|