0Pricing
Java Academy · Lesson

try/catch/finally

Learn to use try, catch, and finally to handle runtime errors safely.

try/catch/finally is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Programs sometimes hit errors: invalid input, missing files, divide by zero. Exceptions are Java's way to handle them without crashing.

try/catch overview

Wrap risky code in a try block. If an exception occurs, control jumps to a matching catch. This lets you handle errors gracefully.

Code: simple try/catch

This code catches an ArithmeticException and prints a friendly message instead of crashing.

public class Main {
  public static void main(String[] args) {
    try {
      int x = 10 / 0; // risky division
      System.out.println("Result=" + x);
    } catch (ArithmeticException e) {
      System.out.println("Cannot divide by zero!");
    }
  }
}

Multiple catches

You can stack multiple catch blocks to handle different exception types separately. Order matters: put specific ones before general ones.

Code: multiple catch

The NullPointerException is caught by its specific block. Each type can be handled differently.

public class Main {
  public static void main(String[] args) {
    try {
      String s = null;
      System.out.println(s.length()); // NullPointerException
    } catch (ArithmeticException e) {
      System.out.println("Math error");
    } catch (NullPointerException e) {
      System.out.println("Null reference error");
    }
  }
}

Code: finally block

The finally block executes whether or not an exception occurs. Use it for cleanup like closing files.

public class Main {
  public static void main(String[] args) {
    try {
      int[] arr = {1,2};
      System.out.println(arr[5]); // ArrayIndexOutOfBounds
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Bad index!");
    } finally {
      System.out.println("Finally block always runs");
    }
  }
}

finally check

Quick check: What is the purpose of the finally block?

Recap

Recap: try handles risky code, catch reacts to errors, and finally runs every time for cleanup.

Frequently asked questions

Is the “try/catch/finally” lesson free?

Yes — the full text of “try/catch/finally” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “try/catch/finally”?

Learn to use try, catch, and finally to handle runtime errors safely. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “try/catch/finally” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. try/catch/finally
  2. Checked vs Unchecked
  3. Reading Stack Traces
← Back to Java Academy