File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes try/catch best practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "try/catch best practice" Watch "try/catch best practice" New topic
Author

try/catch best practice

Dudley Dawson
Ranch Hand

Joined: Dec 14, 2004
Posts: 57
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?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Any objection to: -



Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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.
Brian Spindler
Greenhorn

Joined: May 17, 2007
Posts: 29
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try/catch best practice