• 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

Exception handling

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to Mughal's book once an exception is handled in the catch block then the finally block is executed and the prog continues after the finally block normally
KEEPING this in mind see this code :

import java.io.*;
public class Mine {
public static void main(String argv[]){
Mine m=new Mine();
System.out.println(m.amethod());
}
public int amethod() {
try {
FileInputStream dis=new FileInputStream("Hello.txt");
}catch (FileNotFoundException fne) {
System.out.println("No such file found");
return -1;
}finally{
System.out.println("Doing finally");
}
System.out.println("after finally");
return 0;
}
}

now if no file is there the output is

No such file found
Doing finally
-1

now when the exception is caught the working should proceed normally and println should be executed and 0 should be returned
why does this not happen ?
Thanks in advance
[This message has been edited by Mateen Nasir (edited September 26, 2001).]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mateen
in the catch clause you have a return statement which returns -1. Before returning the finally block is executed and then the method returns -1 and does not go further...
val
 
Mateen Nasir
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The execution will indeed proceed as you explained (try first, if exception then catch, finally after it and then code after finally. finally will get executed irrespective of the fact that exception is caught or not.) However here, when exception is caught "No such file found" is printed on the console, after that "return -1" statement is encountered hence the function must return, but before returning it *must* execute the finally block, so control is transferred to finally block which executes the listed code. after that control gets transferred to the catch block to return to the calling method with return value -1(as it was supposed to return).
If you simply comment the code "return -1" inside catch block you will get following o/p.
<code>
No such file found
Doing finally
after finally
0
</code>

This also makes sense because in real code you may want to detect if function was successful (return value 0) or otherwise (return value -1).
I hope I have not confused you
HTH,
- Manish
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Khalid Explains it very clearly
If the exception is properly handled the flow will be normal
and the program will end correctly
But when we use "return" things change . Why?
Its becoz "return" is a commitment between the caller and the callee
So it will be the last one to be executed by the callee before ending its contract with the caller
If you comment out finally even then it only returns -1
So its not due to the try/catch mechanism.

However there is an exeception to this rule
If you give return on all the three (try/catch/finally)
return value from finally will override all the earlier return
But make sure that return stmt in the finally will the last of all in the entire code.. Why? you will see if you give any statement after return.
Thankyou
Ragu
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS �14.19.2 explains what is supposed to happen.


If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
If the run-time type of V is assignable to the parameter of any catch clause of the try statement, then the first
(leftmost) such catch clause is selected. The value V is
assigned to the parameter of the selected catch clause,
and the Block of that catch clause is executed. Then there is
a choice:
o If the catch block completes abruptly for reason R, then the finally block is executed. Then there is a choice:
o If the finally block completes normally, then the try statement completes abruptly for reason R.


Notice that a return with a value is an abrupt completion (�14.1). So, the return value -1 is what you got.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic