Java: Exception thrown in constructor, can my object still be created?

后端 未结 4 488
感情败类
感情败类 2020-12-16 18:31

Could you tell me can be some case when exception is throwing in constructor and object is not null. I mean some part of object is created and another is not.Like this

4条回答
  •  孤街浪徒
    2020-12-16 19:05

    public Test() {  
        name = "John"; 
        try {
            // exception 
    
            // init some other data.
        } catch(AnyException e) {
            // catch
        }
    }
    

    The above code makes sense as per your expectation.

提交回复
热议问题