Program To Show Usage Of Try, Catch And Finally Statements In Java

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code

Code


To copy code: Click  View Source “View Source” on the right of this code snippet.

Background and Explanation of the code


The program uses try/catch blocks with a finally block. The program has three functions having try-catch-finally statements and that are called one by one. The first function called is procA. It prints a string and throws a runtime exception which is caught by the function main’s catch statement. Now, finally statement is executed since the error has been caught.
Note: The finally statement gets executed in the end when all the errors have been found (and dealt with).