Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good programming to have a return type of Exception?

I ran into an odd situation during a use case on a project: ESQL is calling a java method, sending it a String input parameter which the method will unmarshal, apply some logic, and then store useful info from the unmarshalled object. So, the method must either throw a JAXBException, or use a try catch in order to handle the possible exceptions.

The problem with this is, that ESQL cannot invoke a java method which includes a throws in the signature. BUT, we want any errors to fall through back to the previously calling MBNode so it can be handled appropriately there, so then trycatch is out of the picture.

It struck me that hey, is it not possible to return a type of Exception when we encounter an issue, and null if not? So I wrote a simple method doing so, and though I didn't get any warnings or errors, it seemed just wrong to me in the sense of good programming.

For example:

public Exception doStuffAndCheckForErorrs(String inString)
{
    if(inString.equals(null))
    {
       return new Exception("Your string is null");
    }
    else
    return null;
}

But I just get a terrible feeling about doing anything this way.

I'm open to any thoughts or different solutions to this, especially if there's a way around the ESQL signature issue.

UPDATE:

Adding reference as to why the ESQL procedure cannot call a java method with a throws clause in the signature.

Excerpt from This link under the CREATE PROCEDURE statement section:

"Any Java method that you want to invoke must have the following basic signature: public static (< 0 - N parameters>) where must be in the list of Java IN data types in the table in ESQL to Java data type mapping (excluding the REFERENCE type, which is not permitted as a return value), or the Java void data type. The parameter data types must also be in the ESQL to Java data type mapping table. In addition, the Java method is not allowed to have an exception throws clause in its signature."

like image 870
JWiley Avatar asked Mar 19 '14 15:03

JWiley


People also ask

Which is better a return code or an exception?

An application that uses exceptions is more robust than an application that uses return codes. An application that uses exceptions can also give the cleanest code, since return codes don't have to be checked after every call.

Can an exception be a return type?

If we use exceptions, it's easy. Think of exceptions as a separate return type that gets used only when needed.

Why is using exceptions a better idea than returning an error value?

When you code using return codes, you're preparing yourself for failure, and hope your fortress of tests is secure enough. When you code using exception, you know that your code can fail, and usually put counterfire catch at chosen strategic position in your code.

Should I return null or throw exception?

Only throw an exception if it is truly an error. If it is expected behavior for the object to not exist, return the null. Otherwise it is a matter of preference. It certainly shouldn't be a matter of preference.


2 Answers

This isn't really a question about Java, it's a question about ESQL.

ESQL is able to cope with Java exceptions being thrown through the JNI into ESQL code, you should get a BIP2917 error.

I initially though this might have been an issue with the ESQL method resolver but on IIB v9 I was able to succesfully call the following method:

public static void sayHello() throws Exception{
  System.out.println("hello");
}

This makes me think that perhaps you got something else wrong with your ESQL external function/procedure definition?

like image 125
Dave Avatar answered Oct 09 '22 22:10

Dave


The point here is you can't DECLARE an exception will be thrown; you can still throw a RuntimeException - without adding a throws clause.

So if you wrap your JAXBException into a RuntimeException, you can throw it and handle according to your requirements, without breaking either requirement. Not sure if I would do that; I would not like to return an exception-type though as it's not meant to be used as a return code.

Be extra sure that this asynch way of handling the issue won't break the ESQL library, as you'll be bypassing part of their code, possibly leaving part of it hanging.

like image 42
Calimar41 Avatar answered Oct 09 '22 21:10

Calimar41