Java: pass error by boolean, null pointer or exception?
<p>I think this is an easy but important question.</p>

<p>I am writing a class with a function:</p>

<pre><code>public MyClass myFunction(MyClass mc) { ... }
</code></pre>

<p>In this function, it changes some state of mc and return it back. I understand it is not necessary as mc is change in place. The reason why I want to return MyClass is to use null to indicate an failed update.</p>

<p>I could potentially change the return type to boolean and use false to indicate an error:</p>

<pre><code>public boolean myFunction(MyClass mc) { ... }
</code></pre>

<p>But I remember I have read an article quite long time ago saying this is not a good practice (although I forgot the detail and why).</p>

<p>I could certainly use exception to represent an error:</p>

<pre><code>public void myFunction(MyClass mc) throws MyException { ... }
</code></pre>

<p>But I get a feeling this is too heavy weighted.</p>

<p>My personally opinion is that if the error is meaningful system wide, then exception should be used. If the error is only meaningful for the caller and the function, then exception should not be used. But shall I use null or false to indicate the error in this case?</p>

<p>What is the best practice do you think? Please let me know your opinion.</p>

<p>Many thanks.</p>
