#862 – Options for Including catch and finally Blocks

When you define a try block using the try keyword, you must also do one of the following:

  • Include one or more catch blocks after the try block
  • Include a finally block after the try block
  • Include one or more catch blocks after the try block, followed by a finally block

Whether you include a catch block, a finally block, or both, depends on what you want to accomplish:

  • Include catch blocks to intercept and act upon exceptions that got thrown as a result of executing the code in the try block.
  • Include a finally block if there are things that need to happen (like releasing resources), regardless of whether or not an exception occurs.
  • Include both catch and finally blocks if both statements above are true.
Advertisement