A console-based Java application demonstrating Exception Handling, Object-Oriented Programming, and Collections through a simple library management system.
This project implements a basic library system where users can add, list, borrow, and return books. The core focus of this implementation is to demonstrate how to create and handle Custom Exceptions to manage invalid states and business logic errors gracefully.
| Category | Feature | Description |
|---|---|---|
| 📖 Book Management | Validation | Ensures books have valid titles and positive page counts |
| 🏛️ Library Operations | Add & List | Add books to the library and display the inventory |
| Borrow & Return | Simulate borrowing and returning books by title | |
| Custom Exceptions | Tailored exceptions for specific library errors | |
| Try-Catch Blocks | Robust error handling in the main execution flow |
| Exception Class | Trigger Condition |
|---|---|
InvalidBookException |
Thrown when trying to add a null book to the library |
EmptyLibraryException |
Thrown when listing books or borrowing from an empty library |
BookNotFoundException |
Thrown when trying to borrow/return a book that doesn't exist |
IllegalArgumentException |
Thrown when creating a book with invalid title or page count |
| File | Type | Responsibility |
|---|---|---|
Book.java |
Entity | Represents a book with title and page count validation |
Library.java |
Manager | Handles book collection, borrowing, and returning logic |
BookNotFoundException.java |
Exception | Custom exception for missing books |
EmptyLibraryException.java |
Exception | Custom exception for empty library state |
InvalidBookException.java |
Exception | Custom exception for invalid book objects |
Main.java |
Entry Point | Demonstrates the system workflow and exception handling |
Ensure all files are in the Management package directory, then compile:
javac Management/*.javajava Management.Main(Or run Main.java directly from your IDE)
Library library = new Library();
try {
// Add a valid book
Book book = new Book("Clean Code", 400);
library.addBook(book);
// Borrow the book
library.borrowBook("Clean Code");
} catch (InvalidBookException | BookNotFoundException e) {
System.out.println("Error: " + e.getMessage());
}Library is empty
Title cannot be empty
Page count must be positive
Book should not be null.
Book should not be null.
Books in Library :
Clean Code(400pages)
this book (Clean Code(400pages)) has been successfully loaned
return Cannot. title with Book Clean Code not found
this book (Clean Code(400pages)) has been successfully returned
This project is created for educational purposes to demonstrate Java Exception Handling concepts.
Built with ☕ Java & 🛡️ Custom Exceptions