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

后端 未结 3 517
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 03:26

why does System.out.println(e.getCause()); gives null? And can store whole HashSet collection like this?

private void saving() thro         


        
相关标签:
3条回答
  • 2021-01-01 04:12

    Use printStackTrace() instead of getCause().

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

    0 讨论(0)
  • 2021-01-01 04:16

    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.

    0 讨论(0)
  • 2021-01-01 04:22

    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(); 
    }  
    
    0 讨论(0)
提交回复
热议问题