Hi, The Output of the following program is: No such file found Doing finally -1 but what I expected was No such file found -1 Doing finally Can any one tell me why?
Thanks in advance
(NOTE: I reformatted your code, you put the whole program on one line, which makes it very hard to read and messes up the forum. Please format your code properly next time - Jesper Young, bartender) [ July 16, 2007: Message edited by: Jesper Young ]
SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Well! This is what i have to say. If you have a finally block it will always exceute before method terminates, Having return statement doest not matter. Try to understand the flow. 1)You invoke m.amethod(). 2)You get a FileNotFoundException. 3)You display -------> "No such file found" 4) Since you have a finally block it will exceute before the method terminates [even if there was no exception, finally will always exceute] 5) Now in the FileNotFoundException catch block you return a value -1. Before returning from the method, finally block exceutes In finally block you display -----> ""No such file found"" 5) And finally in main the value -1 is displayed Hence the result is correct. Hope this clears any confusions. Thanks Deepak