Navigation: Main Tutorial Index | Practice Programs | Previous: Strings | Next: Collections
Practice: Practice Programs | Practice Program Index
An exception is an event that occurs during program execution and disrupts the normal flow of instructions.
For example, imagine a program that divides two numbers.
int a = 10;
int b = 0;
int result = a / b;Division by zero is not valid for integer arithmetic in Java. Therefore, the JVM throws an ArithmeticException.
If the exception is not handled, the current thread terminates.
In real-world applications, unexpected situations can occur:
- A user enters invalid input.
- A file does not exist.
- A database connection fails.
- An application tries to divide by zero.
- An array is accessed using an invalid index.
- A network connection becomes unavailable.
Exception handling allows us to handle many such situations and control how the application responds.
- Prevents abrupt termination when an exception is handled appropriately.
- Separates error-handling code from normal business logic.
- Helps maintain application stability.
- Makes debugging easier.
- Allows meaningful error messages.
- Supports reliable resource management.
Java provides a hierarchy for representing exceptional conditions.
Both errors and exceptions are subclasses of the Throwable class.
Object
|
v
Throwable
/ \
/ \
v v
Error Exception
| |
| +--------------------+
| | |
v v v
OutOfMemoryError IOException RuntimeException
SQLException |
|
+--------------+--------------+
| | |
v v v
ArithmeticException
NullPointerException
ArrayIndexOutOfBoundsException
This diagram is simplified. Java contains many additional subclasses.
An Error represents a serious problem that generally occurs outside the normal application-level recovery process.
Examples:
OutOfMemoryErrorStackOverflowErrorVirtualMachineErrorNoClassDefFoundError
Example:
public class StackOverflowExample {
static void display() {
display();
}
public static void main(String[] args) {
display();
}
}The method calls itself without a base case. Eventually, the JVM may throw a StackOverflowError.
Important: Errors are generally not intended to be handled using ordinary application-level exception handling.
An exception is an event that occurs during program execution and interrupts normal program flow.
Examples:
ArithmeticExceptionNullPointerExceptionIOExceptionSQLExceptionNumberFormatException
Example:
public class ExceptionExample {
public static void main(String[] args) {
int a = 10;
int b = 0;
int result = a / b;
System.out.println(result);
}
}Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
The exact stack trace includes additional details such as the source file and line number.
| Error | Exception |
|---|---|
| Generally represents serious system or JVM problems | Represents conditions an application may handle |
| Usually not recoverable through normal application logic | Often recoverable |
| Examples: OutOfMemoryError, StackOverflowError | Examples: IOException, ArithmeticException |
| Subclass of Throwable | Subclass of Throwable |
Exceptions are commonly divided into two main categories:
- Checked Exceptions.
- Unchecked Exceptions.
Checked exceptions are exceptions that the Java compiler checks during compilation.
A method that can produce a checked exception must either:
- Handle it using try-catch, or
- Declare it using
throws, allowing callers to handle it.
Examples:
- IOException
- SQLException
- ClassNotFoundException
- InterruptedException
- FileNotFoundException
import java.io.FileReader;
import java.io.IOException;
public class CheckedExceptionExample {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("data.txt");
reader.close();
} catch (IOException e) {
System.out.println("File operation failed.");
}
}
}Possible Output if the File Does Not Exist:
File operation failed.
The compiler requires handling or declaration of the relevant checked exception.
Unchecked exceptions are exceptions that the compiler does not require you to catch or declare.
They are generally subclasses of RuntimeException.
Examples:
- ArithmeticException
- NullPointerException
- ArrayIndexOutOfBoundsException
- NumberFormatException
- IllegalArgumentException
public class UncheckedExceptionExample {
public static void main(String[] args) {
int a = 10;
int b = 0;
System.out.println(a / b);
}
}Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
| Checked Exception | Unchecked Exception |
|---|---|
| Checked by compiler | Not checked by compiler |
| Must be caught or declared | Catching or declaring is optional |
| Usually subclasses of Exception excluding RuntimeException | Subclasses of RuntimeException |
| Example: IOException | Example: NullPointerException |
Important: Both checked and unchecked exceptions occur at runtime. The difference is whether the compiler requires handling or declaration.
Java provides many predefined exception classes.
| Exception | Description |
|---|---|
| ArithmeticException | Invalid arithmetic operation such as integer division by zero |
| NullPointerException | Attempt to use a null reference in an operation that requires an object |
| ArrayIndexOutOfBoundsException | Invalid array index |
| StringIndexOutOfBoundsException | Invalid String index |
| NumberFormatException | Invalid conversion of text into a numeric value |
| ClassCastException | Invalid runtime type casting |
| IllegalArgumentException | Method receives an illegal or inappropriate argument |
| IOException | Input/output operation failure |
| FileNotFoundException | Requested file cannot be found or opened |
| SQLException | Database access or SQL-related failure |
| ClassNotFoundException | Requested class cannot be located during dynamic class loading |
public class NumberFormatExample {
public static void main(String[] args) {
String value = "Java";
int number = Integer.parseInt(value);
System.out.println(number);
}
}Output:
Exception in thread "main" java.lang.NumberFormatException
The String "Java" cannot be converted into an integer.
The try and catch blocks are used to handle exceptions.
try: Contains code that may throw an exception.catch: Contains code that handles a matching exception.
try {
// Risky code
} catch (ExceptionType e) {
// Exception handling code
}public class TryCatchExample {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int result = a / b;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
}
System.out.println("Program continues.");
}
}Output:
Cannot divide by zero.
Program continues.
- The program enters the try block.
- Java evaluates
10 / 0. - An ArithmeticException occurs.
- The remaining statements in that try block are skipped.
- The matching catch block executes.
- The program continues after the try-catch structure.
A single try block can be followed by multiple catch blocks.
This is useful when different exceptions need different handling.
try {
// Risky code
} catch (ArithmeticException e) {
// Handle arithmetic exception
} catch (ArrayIndexOutOfBoundsException e) {
// Handle array exception
} catch (Exception e) {
// Handle other exceptions
}public class MultipleCatchExample {
public static void main(String[] args) {
try {
int[] numbers = {10, 20, 30};
System.out.println(numbers[5]);
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Arithmetic error.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid array index.");
} catch (Exception e) {
System.out.println("Some other exception occurred.");
}
}
}Output:
Invalid array index.
Why?
The array access throws an exception first. The remaining statements in the try block are skipped.
Specific exception types should be placed before their more general parent exception types.
Incorrect:
try {
// Code
} catch (Exception e) {
// General handling
} catch (ArithmeticException e) {
// Unreachable catch block
}This causes a compilation error because the general catch block already catches ArithmeticException.
Java 7 introduced multi-catch.
It allows multiple exception types to be handled by one catch block using the | operator.
public class MultiCatchExample {
public static void main(String[] args) {
try {
int[] numbers = {10, 20, 30};
System.out.println(numbers[5]);
} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid operation occurred.");
}
}
}Output:
Invalid operation occurred.
Important: The exception types in a multi-catch statement cannot be related through a subclass-parent relationship.
A try block inside another try block is called a nested try block.
It is useful when different operations require separate exception-handling logic.
public class NestedTryExample {
public static void main(String[] args) {
try {
System.out.println("Outer try block.");
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Inner arithmetic exception.");
}
int[] numbers = {10, 20};
System.out.println(numbers[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Outer array exception.");
}
}
}Output:
Outer try block.
Inner arithmetic exception.
Outer array exception.
- The outer try block starts.
- The inner try block causes an ArithmeticException.
- The inner catch block handles it.
- Execution continues in the outer try block.
- An invalid array index causes another exception.
- The outer catch block handles it.
The finally block contains code that normally executes after try-catch processing, whether an exception occurs or not.
It is commonly used for cleanup operations.
try {
// Risky code
} catch (Exception e) {
// Exception handling
} finally {
// Cleanup code
}public class FinallyExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Exception handled.");
} finally {
System.out.println("Finally block executed.");
}
}
}Output:
Exception handled.
Finally block executed.
public class FinallyExample2 {
public static void main(String[] args) {
try {
System.out.println("Inside try block.");
} catch (Exception e) {
System.out.println("Exception occurred.");
} finally {
System.out.println("Finally block executed.");
}
}
}Output:
Inside try block.
Finally block executed.
- The finally block normally executes whether an exception occurs or not.
- It executes after the associated try-catch processing.
- It is commonly used for cleanup.
- It may not execute if the JVM terminates abruptly, such as through
System.exit(), or the process is forcibly terminated. - A finally block can be used without a catch block if it follows a try block.
Best Practice: Use try-with-resources for automatically closing resources that implement AutoCloseable.
These three terms look similar but have different meanings.
| Keyword or Method | Meaning |
|---|---|
final |
Restricts reassignment, overriding, or inheritance |
finally |
Exception-handling block used for cleanup |
finalize() |
Legacy object-finalization method |
public class FinalFinallyExample {
public static void main(String[] args) {
final int number = 100;
try {
System.out.println(number);
} finally {
System.out.println("Cleanup operation.");
}
}
}Output:
100
Cleanup operation.
Note: finalize() is deprecated in modern Java and should not be used for resource management.
The throw keyword is used to explicitly throw an exception from a method or block of code.
It is useful when a programmer wants to indicate that a particular condition is invalid.
throw new ExceptionType("Error message");public class ThrowExample {
public static void main(String[] args) {
int age = 15;
if (age < 18) {
throw new IllegalArgumentException(
"Age must be 18 or above."
);
}
System.out.println("Eligible.");
}
}Output:
Exception in thread "main" java.lang.IllegalArgumentException:
Age must be 18 or above.
public class ThrowExample2 {
public static void main(String[] args) {
try {
int age = 15;
if (age < 18) {
throw new IllegalArgumentException(
"Not eligible to vote."
);
}
System.out.println("Eligible to vote.");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}Output:
Not eligible to vote.
The throws keyword is used in a method declaration to indicate that the method may pass one or more exceptions to its caller.
It is commonly used with checked exceptions.
returnType methodName() throws ExceptionType {
// Method body
}import java.io.FileReader;
import java.io.IOException;
public class ThrowsExample {
static void readFile() throws IOException {
FileReader reader = new FileReader("data.txt");
reader.close();
}
public static void main(String[] args) {
try {
readFile();
} catch (IOException e) {
System.out.println("Unable to read the file.");
}
}
}Possible Output if the File Does Not Exist:
Unable to read the file.
import java.io.IOException;
import java.sql.SQLException;
public class MultipleThrowsExample {
static void performOperation()
throws IOException, SQLException {
// Operations that may throw these exceptions
}
}| throw | throws |
|---|---|
| Used to explicitly throw an exception | Used to declare possible exceptions |
| Written inside a method or block | Written in a method declaration |
| Throws one exception object at a time | Can declare multiple exception types |
| Followed by an exception object | Followed by exception class names |
Example: throw new IOException(); |
Example: void read() throws IOException |
import java.io.IOException;
public class ThrowThrowsExample {
static void checkAge(int age) throws IOException {
if (age < 18) {
throw new IOException("Age restriction.");
}
System.out.println("Allowed.");
}
public static void main(String[] args) {
try {
checkAge(15);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}Output:
Age restriction.
A custom exception is an exception class created by the programmer to represent application-specific error conditions.
Java provides many predefined exceptions, but sometimes they are not meaningful enough for a particular business rule.
For example:
- Insufficient bank balance.
- Invalid employee ID.
- Invalid age.
- Product out of stock.
- Invalid transaction amount.
A custom checked exception generally extends the Exception class.
class InvalidAgeException extends Exception {
InvalidAgeException(String message) {
super(message);
}
}
public class CustomExceptionExample {
static void checkAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException(
"Age must be 18 or above."
);
}
System.out.println("You are eligible.");
}
public static void main(String[] args) {
try {
checkAge(16);
} catch (InvalidAgeException e) {
System.out.println(e.getMessage());
}
}
}Output:
Age must be 18 or above.
A custom unchecked exception generally extends RuntimeException.
class InsufficientBalanceException extends RuntimeException {
InsufficientBalanceException(String message) {
super(message);
}
}
public class CustomUncheckedException {
static void withdraw(double balance, double amount) {
if (amount > balance) {
throw new InsufficientBalanceException(
"Insufficient account balance."
);
}
System.out.println("Withdrawal successful.");
}
public static void main(String[] args) {
try {
withdraw(5000, 7000);
} catch (InsufficientBalanceException e) {
System.out.println(e.getMessage());
}
}
}Output:
Insufficient account balance.
| Custom Checked Exception | Custom Unchecked Exception |
|---|---|
Extends Exception |
Extends RuntimeException |
| Must be caught or declared | Handling or declaration is optional |
| Suitable for conditions callers are expected to handle | Suitable for programming errors or invalid usage, depending on design |
Exception propagation means passing an exception from one method to its caller until a matching catch block handles it.
If no matching handler is found, the exception reaches the thread's uncaught exception handler.
public class ExceptionPropagation {
static void method1() {
int result = 10 / 0;
}
static void method2() {
method1();
}
static void method3() {
method2();
}
public static void main(String[] args) {
try {
method3();
} catch (ArithmeticException e) {
System.out.println("Exception handled in main.");
}
}
}Output:
Exception handled in main.
main()
|
v
method3()
|
v
method2()
|
v
method1()
|
v
ArithmeticException
|
v
Propagates back to method2()
|
v
Propagates back to method3()
|
v
Propagates back to main()
|
v
Caught by main()
Important: Unchecked exceptions can propagate without being declared. Checked exceptions must be handled or declared according to Java's compile-time rules.
Java has special rules for checked exceptions when overriding methods.
A subclass overriding method:
- Can declare the same checked exception.
- Can declare a narrower checked exception.
- Can declare no checked exception.
- Cannot declare a broader checked exception than the overridden method.
import java.io.IOException;
class Parent {
void display() throws IOException {
System.out.println("Parent method.");
}
}
class Child extends Parent {
@Override
void display() throws java.io.FileNotFoundException {
System.out.println("Child method.");
}
}
public class OverrideExceptionExample {
public static void main(String[] args) throws IOException {
Parent obj = new Child();
obj.display();
}
}Output:
Child method.
FileNotFoundException is a subclass of IOException, so this narrower declaration is valid.
Java 7 introduced try-with-resources.
It automatically closes resources after the try block completes.
It is useful for resources such as:
- File streams.
- Readers and writers.
- Database connections.
- Statements and result sets.
- Other objects implementing AutoCloseable.
Consider opening a file or database connection.
After using the resource, we must close it to release system resources.
Without proper cleanup, resources may remain open unnecessarily.
try (ResourceType resource = new ResourceType()) {
// Use resource
} catch (Exception e) {
// Handle exception
}import java.io.FileReader;
import java.io.IOException;
public class TryWithResourcesExample {
public static void main(String[] args) {
try (FileReader reader = new FileReader("data.txt")) {
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
} catch (IOException e) {
System.out.println("File operation failed.");
}
}
}Output if data.txt contains Hello Java:
Hello Java
The FileReader is automatically closed when the try-with-resources statement completes.
try (
FileReader reader = new FileReader("input.txt");
java.io.BufferedReader br =
new java.io.BufferedReader(reader)
) {
System.out.println(br.readLine());
} catch (IOException e) {
System.out.println("File error.");
}Resources are closed automatically in reverse order of their declaration.
In real applications, users may enter invalid data.
We can handle such input using try-catch.
import java.util.Scanner;
public class UserInputException {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.print("Enter a number: ");
int number = sc.nextInt();
System.out.println("Number: " + number);
} catch (java.util.InputMismatchException e) {
System.out.println("Please enter a valid integer.");
} finally {
sc.close();
}
}
}Sample Input:
Enter a number: Java
Output:
Please enter a valid integer.
This example combines:
- Custom exceptions.
- try-catch.
- throw.
- Encapsulation.
- Methods.
class InsufficientBalanceException extends Exception {
InsufficientBalanceException(String message) {
super(message);
}
}
class BankAccount {
private double balance;
BankAccount(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException(
"Deposit amount must be positive."
);
}
balance += amount;
System.out.println("Deposit successful.");
}
public void withdraw(double amount)
throws InsufficientBalanceException {
if (amount <= 0) {
throw new IllegalArgumentException(
"Withdrawal amount must be positive."
);
}
if (amount > balance) {
throw new InsufficientBalanceException(
"Insufficient balance."
);
}
balance -= amount;
System.out.println("Withdrawal successful.");
}
public double getBalance() {
return balance;
}
}
public class BankingApplication {
public static void main(String[] args) {
BankAccount account = new BankAccount(5000);
try {
account.deposit(2000);
account.withdraw(1000);
account.withdraw(10000);
} catch (InsufficientBalanceException |
IllegalArgumentException e) {
System.out.println(e.getMessage());
}
System.out.println("Final Balance: " + account.getBalance());
}
}Output:
Deposit successful.
Withdrawal successful.
Insufficient balance.
Final Balance: 6000.0
| Concept | Application |
|---|---|
| Encapsulation | Private balance |
| Custom Exception | InsufficientBalanceException |
| throw | Explicitly throws an exception |
| throws | Declares a checked exception |
| try-catch | Handles exceptional conditions |
| Methods | Deposit and withdrawal operations |
- Catch specific exceptions instead of catching every exception unnecessarily.
- Do not use empty catch blocks.
- Provide meaningful error messages.
- Do not use exceptions for ordinary control flow.
- Use try-with-resources for AutoCloseable resources.
- Avoid catching Error unless there is a very specific reason.
- Preserve the original cause when wrapping exceptions.
- Do not expose passwords, tokens, or sensitive information in exception messages.
- Log useful diagnostic information where appropriate.
- Create custom exceptions when they improve the clarity of business rules.
- Do not catch an exception and silently ignore it.
- Avoid unnecessarily large try blocks that combine unrelated operations.
Poor practice:
try {
int result = 10 / 0;
} catch (Exception e) {
}Better practice:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
}| Mistake | Explanation |
|---|---|
| Forgetting to handle a checked exception | Causes a compilation error |
| Using a parent exception before a child exception | Makes the child catch block unreachable |
| Writing an empty catch block | Hides failures |
| Confusing throw and throws | They have different purposes |
| Assuming finally always executes | Abrupt JVM termination can prevent it |
| Catching Exception everywhere | Can hide specific problems |
| Forgetting to close resources | May cause resource leaks |
| Creating custom exceptions without meaningful purpose | Makes code unnecessarily complex |
| Catching an exception without handling or propagating it | Can hide application failures |
| Printing only generic messages during debugging | Can make diagnosing problems difficult |
Q1. What is an exception in Java?
Answer: An exception is an event that interrupts the normal flow of program execution.
Q2. What is exception handling?
Answer: Exception handling is a mechanism for detecting and managing exceptional conditions during program execution.
Q3. What is the difference between Error and Exception?
Answer: Error generally represents serious JVM or system-level problems, while exceptions represent conditions an application may be able to handle.
Q4. What are checked exceptions?
Answer: Checked exceptions are exceptions that the compiler requires you to handle or declare.
Q5. What are unchecked exceptions?
Answer: Unchecked exceptions are subclasses of RuntimeException and do not require mandatory handling or declaration.
Q6. What is the purpose of try?
Answer: It contains code that may throw an exception.
Q7. What is the purpose of catch?
Answer: It handles a matching exception thrown from the associated try block.
Q8. What is the purpose of finally?
Answer: It contains cleanup code that normally executes after try-catch processing.
Q9. Can we have multiple catch blocks?
Answer: Yes. A try block can be followed by multiple catch blocks.
Q10. Can we use try without catch?
Answer: Yes. A try block can be followed by finally without a catch block.
Q11. What is the difference between throw and throws?
Answer: throw explicitly throws an exception object, while throws declares exceptions that a method may pass to its caller.
Q12. What is a custom exception?
Answer: A custom exception is an exception class created by the programmer for application-specific conditions.
Q13. What is exception propagation?
Answer: It is the process of passing an exception from a method to its callers until it is handled.
Q14. Can we write a try block inside another try block?
Answer: Yes. This is called nested try.
Q15. Can finally execute if an exception occurs?
Answer: Yes. The finally block normally executes after the matching catch block.
Q16. Can we throw an exception manually?
Answer: Yes. We use the throw keyword.
Q17. What is try-with-resources?
Answer: It is a Java feature that automatically closes resources implementing AutoCloseable.
Q18. What is the difference between Exception and RuntimeException?
Answer: RuntimeException is a subclass of Exception. RuntimeException and its subclasses are unchecked, while other direct and indirect subclasses of Exception are generally checked.
Q19. Can we catch multiple exception types in one catch block?
Answer: Yes. Java supports multi-catch using the | operator.
Q20. Can we override a method and throw a broader checked exception?
Answer: No. An overriding method cannot declare a broader checked exception than the overridden method.
Q21. What happens if an exception is not handled?
Answer: It reaches the thread's uncaught exception handler, which normally prints the stack trace and terminates that thread.
Q22. Can a constructor throw an exception?
Answer: Yes. A constructor can throw checked or unchecked exceptions.
Q23. Can we rethrow an exception?
Answer: Yes. We can catch an exception and throw it again.
Q24. What is exception chaining?
Answer: Exception chaining means preserving an original exception as the cause of another exception.
Q25. What is the difference between getMessage() and printStackTrace()?
Answer: getMessage() returns the exception message, while printStackTrace() prints exception details and the call stack.
Q26. Can a finally block contain a return statement?
Answer: Yes, but it is discouraged because a return from finally can override a return or exception from the try or catch block.
Q27. Is it possible to handle an Error using try-catch?
Answer: Technically yes, because Error extends Throwable, but catching serious Error subclasses is generally not recommended as ordinary recovery.
Q28. What is the purpose of AutoCloseable?
Answer: It defines a close() method for resources that can be automatically closed using try-with-resources.
Q29. What happens when both try and finally throw exceptions?
Answer: If finally throws a new exception while another exception is already propagating, the exception from finally can replace the original exception in ordinary try-finally processing.
Q30. Why should we avoid catching Throwable?
Answer: Throwable includes both Exception and Error. Catching it broadly can hide serious problems that the application is not designed to recover from.
Instructions: Solve these exercises independently. Create separate Java files, test different scenarios, and understand why each exception occurs.
- Write a program to handle ArithmeticException during division.
- Write a program to handle ArrayIndexOutOfBoundsException.
- Write a program to handle NullPointerException.
- Write a program to handle NumberFormatException.
- Write a program using multiple catch blocks.
- Write a program demonstrating the finally block.
- Write a program demonstrating nested try blocks.
- Write a program to accept an integer using Scanner and handle invalid input.
- Write a program demonstrating the throw keyword.
- Write a program demonstrating the throws keyword.
- Create a custom checked exception for invalid age.
- Create a custom unchecked exception for invalid account balance.
- Write a program demonstrating exception propagation through multiple methods.
- Write a program demonstrating multi-catch.
- Write a program to read a file using try-with-resources.
- Write a program demonstrating exception handling in constructors.
- Write a program demonstrating exception handling during method overriding.
- Write a program to validate a user's age using a custom exception.
- Write a program to validate a password and throw an appropriate exception.
- Write a program demonstrating exception chaining.
- Create a Banking Management System with custom exceptions for insufficient balance and invalid transaction amounts.
- Create a Student Registration System with custom exceptions for invalid student details.
- Create a File Management System using try-with-resources.
- Create a Payment Processing System with custom exceptions for failed transactions.
- Create a Login System that handles invalid credentials.
- Design an Employee Management System with exception handling for invalid employee records.
- Create a program that handles multiple file-related exceptions.
- Demonstrate how exceptions propagate through constructors and methods.
- Create a menu-driven calculator with appropriate exception handling.
- Build a small application that combines checked exceptions, unchecked exceptions, custom exceptions, and try-with-resources.
After completing this chapter, you should understand:
- What exceptions are and why exception handling is required.
- The difference between Errors and Exceptions.
- The Java exception hierarchy.
- Checked and unchecked exceptions.
- Common built-in exceptions.
- How try and catch blocks work.
- Multiple catch blocks and multi-catch.
- Nested try blocks.
- The finally block.
- The difference between final, finally, and finalize().
- The throw and throws keywords.
- How to create custom checked and unchecked exceptions.
- Exception propagation.
- Exception handling during method overriding.
- try-with-resources and automatic resource closing.
- Exception handling in real-world applications.
- Best practices for writing reliable Java code.
Chapter 10: Java Collections Framework (JCF)
Topics to explore:
- Introduction to Collections Framework.
- Collection vs Collections.
- List Interface.
- ArrayList.
- LinkedList.
- Vector.
- Stack.
- Set Interface.
- HashSet.
- LinkedHashSet.
- TreeSet.
- Queue Interface.
- PriorityQueue.
- Map Interface.
- HashMap.
- LinkedHashMap.
- TreeMap.
- Iterator and ListIterator.
- Comparable and Comparator.
- Generics in Collections.
- Collections Interview Questions.
- Practice Exercises.
Learning Tip: Exception handling is not just about preventing program crashes. Learn to identify the cause of an exception, choose the correct exception type, and handle it at the appropriate level of your application.