Exceptions and Validation in Java
๐ก Intermediate
๐ Definition
An Exception is an abnormal event occurring during program execution that disrupts the normal flow of instructions. Java provides a robust Exception Handling framework using try, catch, finally, throw, throws, and automatic resource management via Try-with-Resources.
๐ฎ๐ณ Hindi
Program crash hone se bachane ke liye Exception Handling ka use hota hai. Runtime errors ko try...catch block mein handle kiya jata hai. Checked Exceptions compile time par check hote hain (jaise IOException), jabki Unchecked Exceptions (RuntimeException) program execution ke dauran aate hain.
๐ฉ Marathi
Application crash hone pasun vachvanyasathi try...catch block vaparatat. Custom exception tayar karnyasathi extends Exception cha wapar hoto.
๐ 1. The Throwable Class Hierarchy Tree
+--------------------+
| java.lang.Object |
+---------+----------+
|
+---------+----------+
| Throwable |
+----+----------+----+
| |
+-------------+ +------------+
| |
+---------+----------+ +---------+----------+
| Error | | Exception |
+--------------------+ +----+---------------+
(JVM OutOfMemory, |
StackOverflow) +------------------+------------------+
| |
+---------+----------+ +---------+----------+
| Checked Exception | | RuntimeException |
+--------------------+ +--------------------+
(IOException, (NullPointerException,
SQLException) IllegalArgumentException)
- Checked Exceptions: Checked by compiler at compile time. Must be caught (
try-catch) or declared (throws). - Unchecked Exceptions (
RuntimeException): Logical or programming bugs (NullPointerException,ArrayIndexOutOfBoundsException,IllegalArgumentException).
๐ 2. Exception Handling Blocks
try {}: Contains risky code that might throw an exception.catch (ExceptionType e) {}: Catches and handles specific exception types.finally {}: Always executes (whether an exception occurred or not) for cleanup operations.- Try-with-Resources (Java 7+): Automatically closes resources implementing
AutoCloseable(e.g. Streams, Database Connections).
// Multi-Catch Block (Java 7+)
try {
// Risky I/O or Math operations
} catch (IOException | NumberFormatException e) {
System.err.println("Handled multiple error types: " + e.getMessage());
}
๐ 3. Creating Custom Exceptions
// Custom Unchecked Exception
public class InsufficientBalanceException extends RuntimeException {
public InsufficientBalanceException(String message) {
super(message);
}
}
๐ก Complete Example: Account Withdrawal & Input Validation
public class ExceptionMaster {
// Custom Exception
public static class InvalidAmountException extends Exception {
public InvalidAmountException(String message) {
super(message);
}
}
public static class InsufficientFundsException extends RuntimeException {
public InsufficientFundsException(String message) {
super(message);
}
}
// Business Method declaring Checked Exception via 'throws'
public static double processWithdrawal(double balance, String amountStr)
throws InvalidAmountException {
// 1. Validation & Parsing
double amount;
try {
amount = Double.parseDouble(amountStr);
} catch (NumberFormatException e) {
throw new InvalidAmountException("Amount must be a valid numeric number.");
}
// 2. Business Rule Validation
if (amount <= 0) {
throw new InvalidAmountException("Withdrawal amount must be greater than zero.");
}
if (amount > balance) {
throw new InsufficientFundsException(
String.format("Requested โน%.2f exceeds balance โน%.2f", amount, balance));
}
return balance - amount;
}
public static void main(String[] args) {
double currentBalance = 5000.00;
// Test Case 1: Invalid Non-numeric String
try {
processWithdrawal(currentBalance, "abc");
} catch (InvalidAmountException e) {
System.err.println("[TEST 1 ERROR] " + e.getMessage());
}
// Test Case 2: Insufficient Funds (Unchecked)
try {
processWithdrawal(currentBalance, "7500.00");
} catch (InvalidAmountException e) {
System.err.println("[ERROR] " + e.getMessage());
} catch (InsufficientFundsException e) {
System.err.println("[TEST 2 ERROR] " + e.getMessage());
} finally {
System.out.println("Test Case 2 Audit Completed.");
}
}
}
๐ Output
[TEST 1 ERROR] Amount must be a valid numeric number.
[TEST 2 ERROR] Requested โน7500.00 exceeds balance โน5000.00
Test Case 2 Audit Completed.
โ ๏ธ Common Mistakes
- Silently swallowing exceptions with an empty catch block (
catch (Exception e) {}). Always log exceptions! - Catching generic
ThrowableorError(e.g.OutOfMemoryErrorcannot be recovered from gracefully!). - Using exceptions for ordinary control flow logic instead of explicit validation checks (
ifstatements).
๐ก๏ธ Safety / Important Notes
Always use Try-with-Resources when working with file readers, writers, database connections, or socket streams to prevent resource leaks.
๐ Real-World Usage
Validating API request payloads, handling database connection timeouts, catching invalid payment card numbers, and processing missing file inputs.
๐งช Try It Yourself
- Write a method
parseAge(String text)that throwsIllegalArgumentExceptionif age is less than0or greater than120. - Test the method with
"25","-5", and"abc".
๐ฏ Mini Challenge
Create a custom exception InvalidEmailException and write a validation method validateEmail(String email) that throws it if the email does not contain @ or ..
๐ Related Topics
๐งญ Navigation
| โ Java Home | โ Previous: Interfaces | Next: Packages and Project Organization โ |