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

Java · Theory

Java Throw Exception

← All stacks

Theory

94/270

Java Throw Exception

throw actually throws an exception object: throw new IllegalArgumentException("age < 0");. Use it when your method detects a bad situation.

You throw one object. After throw, the next lines in that block do not run. A checked exception must be caught or declared with throws.

Don’t throw null. Don’t throw for normal flow (like “user clicked cancel”). Exceptions are for problems.

Let's take this on the board with one tiny Main class — no extra files. Java Throw Exception — 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 Throw Exception.

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

Write throw new IllegalArgumentException("bad age") and say it stops the method.

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");
    }
  }
}

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

Short notes

  • Defthrow = actually throw an exception object from inside the method.
  • Rulethrow new SomeException("why"); One object at a time.
  • RememberChecked → catch or throws. Unchecked usually not declared.
  • TrapUsing throw for normal flow. Or throw null.

Questions

1

What does throw do?

2

How many objects can throw throw at once?

Previous← Finally Block in JavaNextException Propagation in Java →
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.