File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

understanding try-catch block?

 
Sonali Sehgal
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we write return statement in try catch or finally block.
supose we write return 0 in try catch or finally. is it right or wrong?
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is OK to write return in try{}catch{}finally{} block. We can write return; (when a function is declared as void) or return typeValue; (when a function is declared as not void).


The returned value will be 2.


Result:
Returning value: 1
Returning value: 2
Returned value: 2



Very interesting thing happens when we write return inside finally block:

The above code is OK! The exception is NOT handled, but it is CONSUMED!
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...

Not at all. System.exit(0) will cause that finally will not be executed and internal error/exception of JVM can cause the same
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also finally will suppress any return in catch block.. For instance the below code will print 5.
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot to all of you.I have understood the concept.
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lukas Smith wrote:
Ankit Garg wrote:Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...

Not at all. System.exit(0) will cause that finally will not be executed and internal error/exception of JVM can cause the same


But you don't need to worry about that in SCJP ...
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanmuga Priya wrote:And also finally will suppress any return in catch block.. For instance the below code will print 5.

I have just figured this out

Sonali - you are welcomed!
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
Lukas Smith wrote:
Ankit Garg wrote:Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...

Not at all. System.exit(0) will cause that finally will not be executed and internal error/exception of JVM can cause the same


But you don't need to worry about that in SCJP ...


System.exit(0) can be tested during SCJP. Stay tuned.
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was only trying to say that I've never seen a question combining try catch block and System.exit. So generally you can say that finally block is sure to be executed...

PS: Bert can confirm on that more...
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:I was only trying to say that I've never seen a question combining try catch block and System.exit. So generally you can say that finally block is sure to be executed...

PS: Bert can confirm on that more...


"Never say never..." - from the song

Just a joke
 
Rashmi Liyan
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lukas Smith wrote:It is OK to write return in try{}catch{}finally{} block. We can write return; (when a function is declared as void) or return typeValue; (when a function is declared as not void).


Hi Lukas,
can you exlain this little bit. I didn't get it.

Rashmi.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lukas Smith wrote:
Ankit Garg wrote:
Lukas Smith wrote:
Ankit Garg wrote:Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...

Not at all. System.exit(0) will cause that finally will not be executed and internal error/exception of JVM can cause the same


But you don't need to worry about that in SCJP ...


System.exit(0) can be tested during SCJP. Stay tuned.


You usually put in a finally block the code that must happen no matter how the try block is exited. So thinking of System.exit(0) or JVM internal error/exception is not important for me when i write finally block.
 
Vinay Dinakar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


System.exit(0) makes control to return without executing finally block (Which is having return statement). So now there is no return value, why doesn't compiler throws an error ?
 
Jehaan Butt
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.exit(0) makes control to return without executing finally block (Which is having return statement). So now there is no return value, why doesn't compiler throws an error ?


Well, here your try block is not throwing an exception, due to which there will be no internal JVM error. The System.exit(0) merely exits the program, and that does not cause any error.


This version of code does give an internal JVM error, because there is an internal exception lurking around the JVm which has not been handled.
 
Vinay Dinakar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Thats fine. But my question is , compiler sees that test method return type is "int". But there is no "return" statement gets called here. So Why compiler doesn't say like "return statement is missing" ?
 
Jehaan Butt
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Thats fine. But my question is , compiler sees that test method return type is "int". But there is no "return" statement gets called here. So Why compiler doesn't say like "return statement is missing" ?


My guess (and it is only a guess) is that the compiler doesn't realise that either of the two return calls will not be reached. It sees that the System.exit(0) call is reached and is happy, because when I tried throwing an exception before the call it complained. It does not actually work out the complete code logic and since both the catch and finally have return statements, it feels that there is no problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic