Exception Handling Flashcards

(7 cards)

1
Q

What is an exception?

A

An exception is an error message.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two types of Exceptions?

A

Checked and Unchecked Exceptions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a checked exception?

A

This is a compile time error, that the compiler forces you to handle with the use try/catch blocks and the throw/throws keywords.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

List 3 Examples of checked exceptions.

A
  1. IOException
  2. FileNotFoundException
  3. SQLException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an unchecked exception?

A

These are errors or exception that occur at runtime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some examples of unchecked exceptions?

A

ArithmeticException
ArrayIndexOutOFBoundsException
NullPionterException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Desing and test a custom exception in Java.

A

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);
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly