What is the difference between Finally and Finalize?

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
  }
}

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
  }
}
Default image
Sriram Mannava

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity.

Leave a Reply