Set your Publisher ID in src/config/ads.ts to display live ads.
Exception Handling: Checked vs Unchecked
try-catch-finally, try-with-resources, throws, and custom exceptions
Java distinguishes between Checked Exceptions (enforced by the compiler at build time, e.g. IOException) and Unchecked Exceptions (RuntimeException, e.g. NullPointerException).
Checked vs Unchecked & Try-With-Resources
Checked Exceptions: Subclasses of Exception (excluding RuntimeException). Must be caught in a try-catch block or declared with throws.
Unchecked Exceptions: Subclasses of RuntimeException. Caused by logic bugs; compiler does not force handling.
Try-With-Resources: Automatically closes classes implementing AutoCloseable (like file streams and database connections).
class InsufficientFundsException extends Exception {
public InsufficientFundsException(String msg) {
super(msg);
}
}
public class Bank {
public static void withdraw(double balance, double amount) throws InsufficientFundsException {
if (amount > balance) {
throw new InsufficientFundsException("Cannot withdraw $" + amount + " from balance $" + balance);
}
System.out.println("Withdrawal approved!");
}
public static void main(String[] args) {
try {
withdraw(100.0, 150.0);
} catch (InsufficientFundsException e) {
System.err.println("Transaction Failed: " + e.getMessage());
}
}
}Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which type of exception in Java is checked by the compiler at compile-time, requiring mandatory try-catch or a throws declaration?