why does System.out.println(e.getCause());
gives null
?
And can store whole HashSet collection like this?
private void saving() thro
Use printStackTrace()
instead of getCause()
.
And yes, you can save a whole collection (as long as it holds objects that implement Serilizable).
When an exception is chained, the getCause
method is used to get the original cause. In this case, the exception was not chained from any other layer, hence getCause
returns null
. You should use e.printStackTrace()
instead, to get the reason for the exception. Most likely, this would be because one or more keys/values in your HashSet
are of a type that is not implementing java.io.Serializable
.
Below code gave me exact cause of the exception
try{
// code
}
catch(Exception ex){
Exception exe = new Exception();
String causeString="";
exe.initCause(ex);
if(exe.getCause()!=null && exe.getCause().getCause()!=null)
causeString= exe.getCause().getCause().getLocalizedMessage();
}