• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question about Exception in mock exam

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question below:


The answer is 3)
But i really don't know why -1 is printed at last and why it's -1 but not 0?
If "return -1" is removed,the output will be "No such file found,Doing finally,0"

please explain this code block,thank you !

(indented)
[ August 07, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pan,
Well you seem to be missing a very basic point...

That is that the amethod() method has a return type of int. That's why you are getting -1.

Now for the explanation:
Initially, in the main() method, an object of the Mine class is created as m.

This object m, then calls the method amethod() in the System.out.println() statement.


Now the try block of amethod() executes. It does not find the specified file "Hello.txt" and so it returns a FileNotFoundException which is handled by the first catch block. Hence it prints "No such File Found."

Once this error is handled, the finally block is executed because, the finally block always executes irrespective of whether an exception was thrown or not. So it prints "Doing Finally".

The next statement in the finally block is return -1. Hence you are returning the value -1. Since the return type of amethod() is int, so -1 is returned to the main() method where it is printed.

Hope it clarifies your doubts ?
[ August 08, 2005: Message edited by: Sherry Jacob ]
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sherry Jacob:

The next statement in the finally block is return -1.



Wrong. There is no return statement in the finally block. If there is, output will print whatever it returns.
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by reubin yi:


Wrong. There is no return statement in the finally block. If there is, output will print whatever it returns.




I'm sorry !! My fault.

I meant that the first the error is caught in the catch block...then -1 is returned. But -1 is printed only after it executes the finally block.
 
Pan Zhihua
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
But I haven't known why return -1 is after excuting the finally block?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...please correct me if i'm wrong

when file doesn't exist

method returns a value. It runs in first catch-block and get -1.
After that the message in finally -block was printed out.

code:
...
catch (FileNotFoundException fne) {
System.out.println("No such file found");
return -1;
...

this return value comes with the message in main-method

if file was found
it only runs in the finally - Block and last return 0 statement
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Now, when inside amethod(), the try block starts execution:


However, this file does not exist and hence throws a FileNotFoundException which is caught by the catch block:


And here's your confusion: U asked why -1 is being printed after finally block.

Well, after the catch block is executed, control transfers to the finally block. The finally block will be executed after printing "No such file found" from the catch block.
The -1 which is returned from the catch block will be printed only after the control transfers back to the main() method.

Hence after printing "No such file found" exception, control goes straight to the finally block. where you get:


Lastly, the control transfers back from the amethod() to main(). Hence the last thing done is returning the value -1 from amethod().

So -1 is printed in the end.

Hope now the things are crystal clear !!
[ August 09, 2005: Message edited by: Sherry Jacob ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic