Exception.getCause() returning null when trying to find the source of an exception

百般思念 提交于 2019-11-30 14:41:54

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.

Use printStackTrace() instead of getCause().

And yes, you can save a whole collection (as long as it holds objects that implement Serilizable).

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(); 
}  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!