• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can you have more than one try statement in a java class?  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to know if one can have more than more "try" statement in a java class just in case you need to run different methods at that throws exception. am still a little confused about the exception thing. thank you


Adeiza
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say try it.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pun intended? ;)
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You two really are trying.

Try looking in the Java™ Tutorials.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha no not even
 
Adeiza Yusuf
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Wouter, please forgive my lazy head, I guess am still too excited about the ranch!!

Can you explain what happens when you have only a try and finally statement without a catch after the exception has propagated thru the stack?
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeat:
Wouter Oet wrote:I would say try it.
 
Adeiza Yusuf
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:I repeat:
Wouter Oet wrote:I would say try it.


I think this is more theoretical than practical. its ok if my question can not be answered. thank you for your contributions.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adeiza Yusuf wrote:I think this is more theoretical than practical. its ok if my question can not be answered. thank you for your contributions.


Sounds like a fairly common scenario to me. You want some code to run whether an exception is thrown or not, but you want the calling method to handle the exception.

Write some simple code to get this to happen and if you don't understand what happens when you run it, post the code, along with how what happened differs from what you were expecting and someone will explain it for you.
 
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one bit of friendly advice, never put a return statement in a finally block. It may result in some seriously pathological behaviour. It's good to get into the habit early of not doing it.
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet is right
This one is a practical thing.You can have more than one try statements paired with their respective catch blocks in the code however multiple catch blocks might be associated with a single try block.The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.Finally block is used for keeping the cleanup code.The runtime system always executes the statements within the finally block regardless of what happens within the try block.Single finally block is required per application.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Just one bit of friendly advice, never put a return statement in a finally block. It may result in some seriously pathological behaviour. It's good to get into the habit early of not doing it.

Very true indeed. If you need to return regardless of success or failure, put the return statement after the try-(catch-)finally block.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!