dhblah
dhblah

Reputation: 10151

How should I return a value from function?

Three ways I see here:

1) make function return that value explicitly

2) add additional argument to the function's argument list and pass string holder as that parameter. After the function completes its execution, that string holder will contain the result of the function.

3) throw an exception from that function that will contain the result.

I for myself see first choice more preferable because it's simple, intuitive and straight.

On the other hand, when execution of the function goes in a wrong way there may be additional choices like throw an exception or use a variable passed through parameter to save errors if any happen.

Intuitively, I see that ideal choice is to have explicit return value and throws declaration. So return value is used for normal execution and caught exception will signal about abnormal situation. But I myself don't like exceptions too much if only it is not the completely unpredictable error, so I'd use explicit return value and additional parameter to hold some extra-situation logs or errors.

Please advise books or articles on the subject (it shouldn't necessary talk about Java, any other languages with similar mechanisms are also suitable).

Upvotes: 0

Views: 160

Answers (4)

thkala
thkala

Reputation: 86333

Java methods have return values for a reason: to return their results. In the vast majority of cases #1 should be your only choice. Even if you want to return a wrapper object e.g. because you need to return multiple values, you should probably just return the thing...

Exceptions are just that: they are thrown in exceptional (i.e. abnormal) circumstances. Since traditionally throwing an exception also implies a performance impact, exceptions should not be thrown during normal operation.

Why are you trying to confuse yourself?

EDIT:

On the other hand, exceptions are quite suitable for rare/abnormal conditions:

  • They have built-in support for the generation of debugging information. Getting a stack trace is half the battle in dealing with most bugs.

  • All decent debuggers allow for thrown exceptions to be used as a break point, which can be invaluable.

  • Exceptions allow for error conditions to be passed up the method call chain relatively transparently, allowing for the error handling code to be placed with as much granularity as desired.

  • Exceptions are the standard way to deal with error conditions in Java. This should be enough for most programmers. Please respect the people that will have to work with your code and don't break such basic conventions by reinventing the wheel...

Upvotes: 2

dragn
dragn

Reputation: 1050

Please read Code Complete by Steve McConnell. It's all there.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533492

I would use choice 1 almost every time. There is nothing stopping you throwing a Exception as required.

Upvotes: 1

Jeremiah Orr
Jeremiah Orr

Reputation: 2630

You absolutely want option #1. Exceptions are not unpredictable; if you use them correctly, they are in fact entirely predictable. You can throw different exception types based on the error (FileNotFoundException, IllegalArgumentException, SomeConditionSpecificToMyAppException, etc), and those exceptions can contain any error or diagnostic information you need to convey.

Here's a good section in the Java SE tutorial on exceptions: http://docs.oracle.com/javase/tutorial/essential/exceptions/

Upvotes: 5

Related Questions