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

Java · Theory

Custom Exception in Java

← All stacks

Theory

100/270

Custom Exception in Java

A custom exception is your own error type: class AgeException extends Exception { ... }. Use it when a business rule fails (age < 0, balance too low) and you want a clear name.

Extend Exception for checked (caller must handle). Extend RuntimeException for unchecked. Give a constructor that takes a message: super(msg).

Don’t invent a custom exception for every tiny check. Use it when the name helps the caller.

Let's take this on the board with one tiny Main class — no extra files. Output: Asha scored 72 then Pass. Real Main. Swap marks/name to show one idea from Custom Exception in Java. Start from main, go line by line, and stop at each print. That output is the proof for Custom Exception.

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

Write class AgeException extends Exception { AgeException(String m) { super(m); } } then throw it.

Example

// Custom
public class Main {
  public static void main(String[] args) {
    int marks = 72;
    String name = "Asha";
    System.out.println(name + " scored " + marks);
    if (marks >= 40) System.out.println("Pass");
  }
}

Output: Asha scored 72 then Pass. Real Main. Swap marks/name to show one idea from Custom Exception in Java.

Short notes

  • DefCustom exception = your own error class for a business rule.
  • Ruleextends Exception (checked) or RuntimeException (unchecked).
  • RememberConstructor with message → super(msg).
  • UseAgeException, InsufficientFundsException — clear names.

Questions

1

When do you make a custom exception?

2

Exception vs RuntimeException parent?

Previous← Exception Handling with Method OverridingNextInner Classes 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.