Java – why are exception error strings different in different environments?

I have this code to handle catching specific exceptions

private static final String CONNECTION_REFUSED_EXCEPTION = "java.net.ConnectException: Connection refused: connect";
...
} catch (org.apache.axis.AxisFault af) {

            if (af.getFaultString().equals(CONNECTION_REFUSED_EXCEPTION))
            {
               // Do something
            }
}

This works locally in my windows development environment

However, when I deploy to a UNIX machine for testing, the fault string will be different as follows (Note: the missing connection starts at the end)

>Windows fault string: Java net. Connectexception: connection rejection: connection > UNIX fault string: Java net. Connectexception: connection denied

Why is that?

For record purposes, I believe the following is more suitable for matching fault strings:

...
if(af.getCause() instanceof java.net.ConnectException)
...

Solution

The "fault string" message is intended to provide additional information for debugging purposes They can vary in different implementations, versions, or environments The same applies to all exception message strings (throwable. Getmessage())

Application code should not rely on the exact text of the exception message to identify the cause of the exception You suggest using instanceof to check the cause of the exception is a better solution

Edit:

If you absolutely need to check the detailed message because it seems to happen in your case (because getcause() returns null), I recommend checking whether the detailed message contains the name of the specific wrapper exception you want to check (for example, Java. Net. Connectexception) If getCause () does not return null first, this will be the same information you get, and you should minimize dependencies with the exact text of the detailed message

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>