PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Exception Handling in Java

← All stacks

Theory

89/270

Exception Handling in Java

Exceptions are problems at runtime. Handling them keeps the app from crashing blindly. try holds risky code (divide, file, parse). catch runs backup code if that problem happens. finally runs cleanup whether error happened or not — close files, close connections.

Two families: checked (must handle or declare) and unchecked (RuntimeException family). Catch specific exceptions and give a clear message. Empty catch blocks hide bugs.

Explain try / catch / finally, then checked vs unchecked in one line each.

Let's take this on the board with one tiny Main class — no extra files. Exception Handling in Java — output: Cannot divide by zero. 10/0 throws ArithmeticException; catch prints the message. x is never printed. Start from main, go line by line, and stop at each print. That output is the proof for Exception Handling.

Diagram
try { risky }
        │ throws
        ▼
  catch → message
        │
        ▼
  finally → cleanup
Exam tip

try/catch/finally + checked vs unchecked.

Example

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

Exception Handling in Java — output: Cannot divide by zero. 10/0 throws ArithmeticException; catch prints the message. x is never printed.

Short notes

  • DefException = runtime problem. Handle it so the app does not crash blindly.
  • Ruletry = risky code. catch = backup. finally = cleanup always.
  • Diffchecked = must handle/declare. unchecked = RuntimeException family.
  • TrapEmpty catch hides the real bug.

Questions

1

What goes in try?

2

What does finally do?

3

Checked vs unchecked?

Previous← Java Regular Expressions (Regex)NextJava try-catch Block →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.