Skip to content
Learn Netverks

Lesson

Step 24/36 67% through track

exceptions-try-catch

Exceptions and try-catch

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Exceptions and try-catch: the syntax, APIs, and habits you need before advancing in Java.

Checked vs unchecked exceptions shape API design—misuse creates noisy try/catch or swallowed failures.

You will apply Exceptions and try-catch in contexts like: Spring Boot APIs, banking systems, Android (with Kotlin), and batch/data pipelines on the JVM.

Write Java with a public class (lessons use Main), click Run on server—the dev runner runs javac then java; fix compile errors from stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Java uses exceptions for error signaling. Use try/catch/finally to handle recoverable failures; uncaught exceptions terminate the thread and print a stack trace.

Basic syntax

try {
    int n = Integer.parseInt("42");
} catch (NumberFormatException e) {
    System.out.println("Bad number: " + e.getMessage());
} finally {
    System.out.println("Always runs");
}

Throwing

if (amount < 0) {
    throw new IllegalArgumentException("amount must be positive");
}

Important interview questions and answers

  1. Q: Exception vs Error?
    A: Exceptions are recoverable application issues; Errors are serious JVM problems (OutOfMemoryError)—do not catch casually.
  2. Q: finally without catch?
    A: Valid—try/finally with try-with-resources preferred for I/O.

Self-check

  1. Which block always runs (usually)?
  2. When should you throw IllegalArgumentException?

Tip: Catch specific exceptions first—avoid empty catch (Exception e) {} blocks that swallow errors silently.

Interview prep

Checked vs unchecked?

Checked exceptions extend Exception (not RuntimeException) and must be caught or declared; unchecked extend RuntimeException.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • finally always runs?
  • try-with-resources win?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump