Search code examples
javaexceptionconventions

Would it be a good idea to pass an Error object by reference rather than throwing exceptions from a method?


I am writing a Utility API for a company and I'm considering that, rather than the utility methods throw Exceptions, a custom empty Error object is passed to method by reference and is populated in the event of an exception.

UML Class Diagram

ABCError

-code: long
-message: String
-throwable: Throwable

-ABCError()
-set(code,message,throwable) : void
-set(code,message) : void
-set(code):void
-set(message):void
-isSet() : boolean

Usage:

ABCError error = new ABCError();
HttpResponse response = ABCHttpUtils.post(url,headers,parameters,error);
if(!error.isSet()){
//...
}

Is this a good idea or a bad one because it breaks Java Coding Convention?


Solution

  • It's definitely non-idiomatic Java; but the major reason your code won't work is that java is only pass by value - there are no C++-style references, there are no OUT parameters.

    Edit: Oh, wait, you don't use references per se in your code - ignore that. However, I would urge you not to use the optional error parameter, as you force the API clients to check all invocations for the return codes, instead of in a catch/finally block.