Basic: Java return code or throwing exceptions

Sometimes you have this basic question that you  are too shy to ask around because you should know. Reality is that I always have a few of those and eventually you have to look around for what others are doing if you want to get better.

I just completed some code and I was wondering if using the return code for errors was a good idea or if I should have used exceptions everywhere.

After reading a few articles I think that I did the right thing.

One key sentence that I like is: exceptions are for exceptional conditions. So you should see an exception if the failure is not expected and abnormal to the working of your application. A good point.

Others are saying that you should crash loudly. They are saying to use exceptions as much as possible so that it gets your attention and you fix the issue sooner than later.

The argument that return code makes the application more stable seems to be a bit simplistic because you might be hiding issues that will be more difficult to identify later.

There is room for both approach and I think that experience tells you what to do. Always using one or the other does not seem to make sense. Balance.

Return code makes sense right now because this Java code needs to integrate with some Bash code. If the solution was purely Java I think that I would need to change this strategy.

I may start using more checked exceptions since it gives me a better sense of balance and allows to have more information about the failure.

Continuous improvements.

Advertisements

Published by

m5c

Java developper that loves photography and good coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s