Unit 7: Exeptions Flashcards
What is an “exception” in Java?
An event that occurs during the execution of a program that disrupts the normal flow, like a file not found or invalid number input.
Give examples of common exceptions in Java.
FileNotFoundException
ArrayIndexOutOfBoundsException
NumberFormatException
What happens if an exception is not handled?
The JVM reports the exception and skips to the next event — the program may miss actions or become unreliable.
Why should we handle exceptions instead of ignoring them?
To avoid crashes, ugly error messages, or continuing with unreliable/partial data — important in professional software.
What is the basic structure for handling exceptions in Java?
try {
// risky code
} catch (ExceptionType e) {
// handle it
}
When does the catch block run?
Only if an exception is thrown in the try block — otherwise, it’s skipped.
Why not always check for errors before running risky code?
Sometimes it’s hard or impossible to predict if an error will happen — using try-catch is more reliable.