Chapter 11 Flashcards
(24 cards)
What is a syntax error in Java?
An error where the code violates Java syntax rules. Caught by the compiler.
What is a logic error in Java?
Code compiles and runs but does not produce the intended result.
What is a runtime error in Java?
An error that occurs while the program is running, causing an exception to be thrown.
What is an exception in Java?
An event that disrupts the normal flow of a program’s instructions, often due to unexpected inputs or conditions.
Why use exceptions instead of if-else for error checking?
To separate error handling from regular logic and reduce code clutter.
What does the ‘try’ block do in Java?
Encapsulates code that might throw an exception.
What does the ‘catch’ block do in Java?
Handles exceptions thrown from the try block.
What does the ‘throw’ statement do?
Creates and throws a new exception object.
Do you always need to use the ‘throw’ keyword?
No. You only use ‘throw’ when you want to manually trigger an exception.
Can you create custom exceptions in Java?
Yes, by extending the Exception class.
What is the root class of all throwable types in Java?
Throwable
What are the two main subclasses of Throwable?
Error and Exception
What are checked exceptions?
Exceptions that must be declared or handled; compiler-enforced.
What are unchecked exceptions?
Runtime exceptions not enforced by the compiler.
Give examples of checked exceptions.
IOException, FileNotFoundException
Give examples of unchecked exceptions.
NullPointerException, ArithmeticException, IndexOutOfBoundsException
Can you catch multiple exception types in Java?
Yes, by using multiple catch blocks or a multi-catch block with ‘|’
What happens when a method throws an exception and doesn’t catch it?
It is passed up to the calling method.
Is it cleaner to handle exceptions in the main method?
Yes, it centralizes error handling and keeps methods focused.
What is a ‘finally’ block in Java?
A block that always executes after try/catch, used for cleanup.
Why must you catch FileNotFoundException?
It is a checked exception and must be handled or declared. Also the file might actually not be able to be found
What does getMessage() do?
Returns the detail message of the exception.
What does printStackTrace() do?
Prints the full stack trace to help with debugging.
What does getCause() return?
Returns the underlying cause of the exception.