Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes design choice: to return from a catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "design choice: to return from a catch block" Watch "design choice: to return from a catch block" New topic
Author

design choice: to return from a catch block

Mohit Sinha
Ranch Hand

Joined: Nov 29, 2004
Posts: 125
Hi there,

I would like to know your thoughts if using a statement like "return output;" in the catch block is a good design practice.
The design contract we have with the external calling entity is to populate the output object with values and success message in case of success and error code and error message in case of failure.
We do not propagate a java exception back to the external calling entity but instead capture the details of the exception in the catch block, populate the output object with the relevant exception details and send it back to the external calling entity.
Now my question is this design approach right to return back from a catch block. This way I am actually trying to exit from the particular flow of the program.
There are also scenarios (some catch blocks) where we will not return i.e exit the program flow. Instead just log the exception and proceed with the next step in the flow.

Do let me know your thoughts.

Regards,
Mohit
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Yes, it's a totally valid coding practice, although I would advise to use it carefully.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: design choice: to return from a catch block