STUDYECRAT Java :Custom Exception
60s
Java Custom Exception
Prove your skills in this interactive quiz
Live Code
Run snippets directly
Timed
60s per question
Scored
Earn 3D badges
★Java Custom Exception : Key Interview Points
1. Checked vs Unchecked Exceptions
// Checked (compile-time) exception
void readFile() throws IOException { ... }
// Unchecked (runtime) exception
void divide(int a, int b) {
if (b == 0) throw new ArithmeticException();
} // Compiler forces handling for checked exceptions only
void readFile() throws IOException { ... }
// Unchecked (runtime) exception
void divide(int a, int b) {
if (b == 0) throw new ArithmeticException();
} // Compiler forces handling for checked exceptions only
- Tip: Use unchecked exceptions for programming errors (e.g., null checks).
- Real Use: File I/O operations mandate handling
IOException
.
2. Creating Custom Exceptions
class InvalidAgeException extends Exception {
InvalidAgeException(String msg) {
super(msg);
}
}
void validate(int age) throws InvalidAgeException {
if (age < 18) throw new InvalidAgeException("Age < 18");
} // Extend Exception for checked, RuntimeException for unchecked
InvalidAgeException(String msg) {
super(msg);
}
}
void validate(int age) throws InvalidAgeException {
if (age < 18) throw new InvalidAgeException("Age < 18");
} // Extend Exception for checked, RuntimeException for unchecked
- Tip: Always provide constructors with error messages for debugging.
- Real Use: Domain-specific validations (e.g., banking rules).
3. Try-With-Resources (Java 7+)
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
} // Auto-closes resources implementing AutoCloseable
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
} // Auto-closes resources implementing AutoCloseable
- Tip: Prefer this over manual
finally
blocks for resource cleanup. - Real Use: Database connections or file handlers.
4. Exception Propagation
void methodA() {
methodB(); // Unchecked exception propagates up
}
void methodB() {
throw new NullPointerException();
}
public static void main(String[] args) {
try { new Test().methodA(); }
catch (NullPointerException e) { System.out.println("Caught"); }
} // Unchecked exceptions bubble up the call stack
methodB(); // Unchecked exception propagates up
}
void methodB() {
throw new NullPointerException();
}
public static void main(String[] args) {
try { new Test().methodA(); }
catch (NullPointerException e) { System.out.println("Caught"); }
} // Unchecked exceptions bubble up the call stack
- Tip: Handle exceptions at the layer where recovery is possible.
- Real Use: Framework-level error handling (e.g., Spring MVC).
5. Multi-Catch Block
try {
// Code that throws multiple exceptions
} catch (IOException | SQLException e) {
System.out.println("Database/File error: " + e.getMessage());
} // Pipe (|) syntax to catch multiple exceptions
// Code that throws multiple exceptions
} catch (IOException | SQLException e) {
System.out.println("Database/File error: " + e.getMessage());
} // Pipe (|) syntax to catch multiple exceptions
- Tip: Use when handling logic is identical for both exceptions.
- Real Use: CRUD operations with file + DB dependencies.
💡 Pro Interview Tip
When asked about exception handling, always differentiate between checked (recoverable) and unchecked (bugs) exceptions. Mention real-world examples like FileNotFoundException
(checked) vs NullPointerException
(unchecked).