Exception message is null? Exception message is null? java java

Exception message is null?


The message and the stacktrace are two distinct pieces of information. While the stackstrace is mandatory, the message is not. Most exceptions carry a message, and it is the best practice to do so, but some just don't and there's nothing to be done to fix it.

You can make it easier for your clients though and provide a message by wrapping the message-less exception or throwing a custom exception with the original exception as the cause. This might look like following.

throw new  MyRuntimeException("Socket was closed unexpectedly", e);


SocketTimeOutException is the type of the exception. It's probably just that this exception (or the code throwing it) didn't bother providing a message with the exception, and thought the type of the exception was sufficiently meaningful. Use

Log.d("Error", "Error Message: " + e);

or

Log.d("Error", "Some exception occurred", e);

rather than asking the message.


e.printstacktrace() means the actual stacktrace which is almost always present. A message however not. If you check the Javadoc of the getMessage() method, you will notice that it may return null. There will be message if you call the constructor from exception with the String message param Exception Javadoc