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 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.
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.
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.
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.
Post by:autobot
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking