Exception Handling Flashcards
(7 cards)
What is an exception?
An exception is an error message.
What are the two types of Exceptions?
Checked and Unchecked Exceptions
What is a checked exception?
This is a compile time error, that the compiler forces you to handle with the use try/catch blocks and the throw/throws keywords.
List 3 Examples of checked exceptions.
- IOException
- FileNotFoundException
- SQLException
What is an unchecked exception?
These are errors or exception that occur at runtime
What are some examples of unchecked exceptions?
ArithmeticException
ArrayIndexOutOFBoundsException
NullPionterException
Desing and test a custom exception in Java.
Custom Exception:
public class InvalidBookIdException extends Exception {
public InvalidBookIdException(String message) {
super(message);
}
}
testing:
import java.util.HashMap;
import java.util.Map;
public class Library {
private Map<String, String> books = new HashMap<>();
public Library() { books.put("B001", "Java Programming"); books.put("B002", "Data Structures"); } public String getBookById(String id) throws InvalidBookIdException { if (!books.containsKey(id)) { throw new InvalidBookIdException("Book ID " + id + " not found."); } return books.get(id); } }