Program To Show Usage Of Try And Catch Statements In Java

LEVEL: Beginner

Whats in this post?

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


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

Background and Explanation of the code

This program uses the main class and inputs a number. If the number is zero, an exception is shown Divide by zero. Also shown as an example here is an exception that occurs due to array index being out of bounds i.e. an exception that occurs when the user tries to insert data into an array at an index that is greater than the size of the array.