Error handling
- Java course
- Beginner
- Lesson 115/116
- 6 min
Add input validation with try/catch.
Throw when no book is available
When borrowBook finds no available book, it should throw an error instead of ending silently. You use throw new IllegalStateException to inform the caller.
void borrowBook() {
for (Book b : books) {
if (b.available) {
b.available = false;
return;
}
}
throw new IllegalStateException("Müsait kitap yok");
}After the loop completes without return, no book is available. At this point throw fires. The caller catches it with try/catch; if not caught, the program crashes. This is the foundation of error handling.
try/catch in main
Since borrowBook can now throw, you must use try/catch when calling it. The second borrow attempt throws, and the error is caught and printed.
try {
cat.borrowBook();
cat.borrowBook();
} catch (IllegalStateException e) {
System.out.println("Hata: " + e.getMessage());
}The first borrowBook succeeds, the second throws. catch catches the error and getMessage() retrieves the message. This structure lets you handle the error without crashing the program.
Practice Throw and catch the error
Add throw new IllegalStateException("Müsait kitap yok") at the end of Catalog.borrowBook (after the loop). In main call cat.borrowBook() twice inside try; catch IllegalStateException and print Hata: with getMessage().
Starter code
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Catalog cat = new Catalog();
cat.addBook("Suç ve Ceza");
// Call borrowBook twice; the second throws, catch with try/catch
}
}
class Book {
String title;
boolean available;
Book(String title) {
this.title = title;
this.available = true;
}
public String toString() {
return (available ? "[müsait] " : "[ödünç] ") + title;
}
}
class Catalog {
ArrayList<Book> books = new ArrayList<>();
void addBook(String title) {
books.add(new Book(title));
}
void borrowBook() {
for (Book b : books) {
if (b.available) {
b.available = false;
return;
}
}
// No available book reaches here
}
}Sample solution
// At the end of Catalog.borrowBook (after the loop):
throw new IllegalStateException("Müsait kitap yok");
// inside main:
try {
cat.borrowBook();
cat.borrowBook();
} catch (IllegalStateException e) {
System.out.println("Hata: " + e.getMessage());
}Expected output
Hata: Müsait kitap yok
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What happens when borrowBook finds no available book?
- Ends silently
- Throws IllegalStateException Correct answer
- Deletes all books
- Closes the program
Explanation: after the loop, throw new IllegalStateException fires; if the caller does not catch it, it crashes.
2 Match each keyword to its purpose
| Term | Meaning |
|---|---|
throw |
Throws exception |
try |
Risky code |
catch |
Catch error |
IllegalStateException |
Error type |
3 Complete the catch block
try {
cat.borrowBook();
} catch (____ e) {
System.out.println("Hata: " + e.getMessage());
}
try {
cat.borrowBook();
} catch (IllegalStateException e) {
System.out.println("Hata: " + e.getMessage());
}
Explanation: the type borrowBook throws is IllegalStateException.