Tsallaka zuwa abun ciki
Justice Tech School

try, catch, finally

Handle errors with try catch. finally always runs. throw creates an exception.

Example
public class Main {
  public static void main(String[] args) {
    try {
      int[] a = new int[2];
      a[5] = 1;
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("caught");
    } finally {
      System.out.println("done");
    }
  }
}

Result

caught
done

Exercise

Always runs.

try {}  {}

Quiz Test

Are checked exceptions required to be handled?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.