Chapter 11 Flashcards

(24 cards)

1
Q

What is a syntax error in Java?

A

An error where the code violates Java syntax rules. Caught by the compiler.

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

What is a logic error in Java?

A

Code compiles and runs but does not produce the intended result.

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

What is a runtime error in Java?

A

An error that occurs while the program is running, causing an exception to be thrown.

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

What is an exception in Java?

A

An event that disrupts the normal flow of a program’s instructions, often due to unexpected inputs or conditions.

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

Why use exceptions instead of if-else for error checking?

A

To separate error handling from regular logic and reduce code clutter.

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

What does the ‘try’ block do in Java?

A

Encapsulates code that might throw an exception.

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

What does the ‘catch’ block do in Java?

A

Handles exceptions thrown from the try block.

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

What does the ‘throw’ statement do?

A

Creates and throws a new exception object.

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

Do you always need to use the ‘throw’ keyword?

A

No. You only use ‘throw’ when you want to manually trigger an exception.

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

Can you create custom exceptions in Java?

A

Yes, by extending the Exception class.

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

What is the root class of all throwable types in Java?

A

Throwable

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

What are the two main subclasses of Throwable?

A

Error and Exception

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

What are checked exceptions?

A

Exceptions that must be declared or handled; compiler-enforced.

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

What are unchecked exceptions?

A

Runtime exceptions not enforced by the compiler.

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

Give examples of checked exceptions.

A

IOException, FileNotFoundException

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

Give examples of unchecked exceptions.

A

NullPointerException, ArithmeticException, IndexOutOfBoundsException

17
Q

Can you catch multiple exception types in Java?

A

Yes, by using multiple catch blocks or a multi-catch block with ‘|’

18
Q

What happens when a method throws an exception and doesn’t catch it?

A

It is passed up to the calling method.

19
Q

Is it cleaner to handle exceptions in the main method?

A

Yes, it centralizes error handling and keeps methods focused.

20
Q

What is a ‘finally’ block in Java?

A

A block that always executes after try/catch, used for cleanup.

21
Q

Why must you catch FileNotFoundException?

A

It is a checked exception and must be handled or declared. Also the file might actually not be able to be found

22
Q

What does getMessage() do?

A

Returns the detail message of the exception.

23
Q

What does printStackTrace() do?

A

Prints the full stack trace to help with debugging.

24
Q

What does getCause() return?

A

Returns the underlying cause of the exception.