Forums Register Login

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
I would say try it.
+Pie Number of slices to send: Send
Pun intended? ;)
+Pie Number of slices to send: Send
You two really are trying.

Try looking in the Java™ Tutorials.
+Pie Number of slices to send: Send
Haha no not even
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
I repeat:

Wouter Oet wrote:I would say try it.

 
+Pie Number of slices to send: Send
 

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.
1
+Pie Number of slices to send: Send
 

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.
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 1274 times.
Similar Threads
ResultSet
Why we throw exception as we have try and catch to catch the Exception ?
local variables in the catch statement
JDBC Code Optimization Question
finally { } clause in Exceptions
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:36:53.