Can finalize be called after a constructor throws an exception?

前端 未结 3 843
青春惊慌失措
青春惊慌失措 2021-01-01 17:06

Are there any details on whether or not an object is cleaned up using finalize() if that object\'s constructor thew an exception.

When this method is ca

3条回答
  •  猫巷女王i
    2021-01-01 17:15

    According to section 12.6.1. Implementing Finalization of the JLS:

    An object o is not finalizable until its constructor has invoked the constructor for Object on o and that invocation has completed successfully (that is, without throwing an exception).

    If your constructor throws an exception after the Object constructor completes, then your object should be finalizable, so finalize() could still be called.

    There's a good example stepping through object construction in section 12.5. Creation of New Class Instances that shows exactly when the Object constructor is called.

提交回复
热议问题