Java Exceptions: 18 flashcards
All 18 flashcards from this kit, free and with no sign-up.
1. What is an exception in Java?
Show the answer
An exception is an event that disrupts the normal flow of a program's instructions, often representing an error condition.
2. What is the purpose of exception handling in Java?
Show the answer
To handle runtime errors gracefully so the program can continue or terminate cleanly, rather than crashing abruptly.
3. Name the five keywords used in Java exception handling.
Show the answer
try, catch, finally, throw, throws
4. What is the role of the 'try' block?
Show the answer
It encloses code that might throw an exception. It must be followed by at least one catch or a finally block.
5. What is the role of the 'catch' block?
Show the answer
It handles the exception thrown in the try block. It must specify the exception type it can catch.
6. What is the role of the 'finally' block?
Show the answer
It always executes (unless System.exit() is called) regardless of whether an exception occurs or not. Used for cleanup.
7. What is the difference between 'throw' and 'throws'?
Show the answer
'throw' is used to explicitly throw an exception (inside a method). 'throws' is used in the method signature to declare that a method might throw exceptions.
8. What is the difference between checked and unchecked exceptions?
Show the answer
Checked exceptions are checked at compile-time (must be handled or declared). Unchecked exceptions (RuntimeException and its subclasses) are not checked at compile-time.
9. Give two examples of checked exceptions.
Show the answer
IOException, SQLException (also ClassNotFoundException, InterruptedException).
10. Give two examples of unchecked exceptions.
Show the answer
ArithmeticException, NullPointerException (also ArrayIndexOutOfBoundsException, NumberFormatException).
11. What is the 'finally' block used for?
Show the answer
For cleanup activities like closing files, releasing resources, or releasing locks, ensuring they run regardless of exceptions.
12. What is the difference between 'Exception' and 'Error' in Java?
Show the answer
Exception represents conditions that a program should catch and handle (like IOException). Error represents serious problems that are not meant to be caught (like OutOfMemoryError).
13. What is the purpose of the 'getMessage()' method in an exception object?
Show the answer
It returns a detailed message about the exception that occurred, often set when the exception was thrown.
14. What is the purpose of the 'printStackTrace()' method?
Show the answer
It prints the stack trace of the exception, showing the sequence of method calls that led to the exception.
15. What is multi-catch in Java?
Show the answer
A feature that allows a single catch block to handle multiple exception types, separated by a pipe (|). Example: catch (IOException | SQLException e).
16. What is try-with-resources?
Show the answer
A try statement that automatically closes resources (like files or streams) that implement AutoCloseable, after the try block finishes.
17. What is an exception handler?
Show the answer
A catch block that handles a specific type of exception. It is the code that responds to the exception.
18. What is the difference between 'throw' and 'throws'?
Show the answer
'throw' is used to explicitly throw an exception (inside a method). 'throws' is used in the method signature to declare that a method might throw exceptions.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages