Why do we use finally blocks? [duplicate]

  • What happens if an exception you’re not handling gets thrown? (I hope you’re not catching Throwable…)
  • What happens if you return from inside the try block?
  • What happens if the catch block throws an exception?

A finally block makes sure that however you exit that block (modulo a few ways of aborting the whole process explicitly), it will get executed. That’s important for deterministic cleanup of resources.

Leave a Comment