What is the difference between Finally and Finalize?

Finally is a block that is used within exception handling. Finalize is a method in conjunction with the Garbage Collection.

Finally is a block that is used in conjunction with the try-catch-finally or the try-finally blocks for exception handling. Any lines of code residing under the finally block are executed even if there is an Exception inside the try block.
For example, Stream close or DbConnection close statements.

db.open();
try {
// some statements
}
catch(Exception ex) {
// handle exception
}
finally {
 db.close();
}

Finalize is a method in conjunction with the Garbage Collection. The method is called by the GC to cleanup resources before the object is destroyed.

public class MyClass {
  ~MyClass() {
    // any custom release operations
  }
}

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *