Forums Register Login

try/catch best practice

+Pie Number of slices to send: Send
hi- i have a method that returns a string, which is derived from another method that throws an exception:


p.process() throws an exception. if I place it within a try/catch block, then val is only in scope within that try/catch block, and I cannot return it outside of the block. If i put the return inside the block, I then have to put some other default return outside the block. Seems sloppy. Is there an elegant way to do this?
+Pie Number of slices to send: Send
Any objection to: -

+Pie Number of slices to send: Send
I would prefer



If need be initalize val to a default value.

It depends a lot on the program flow as well. Throwing the exception forward may also be a good idea depedning on where should the exception be handled.
+Pie Number of slices to send: Send
Yeah I agree with Anupam, if the exception is handled elsewhere you could write:



Or the same with handling in the method:


[ May 17, 2007: Message edited by: Brian Spindler ]
This one time, at bandcamp, I had relations with a 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 724 times.
Similar Threads
Unreachable statement
Return from finally
where to use return statement
Exception Handling
Does CallableStatement have to be closed?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:30:17.