exception handling Flashcards
(53 cards)
What is an exception?
An exception is an error or unexpected situation that happens while the program is running.
What are the common causes of exception?
- User enters the wrong data type
- Dividing a number by zero
- Accessing an array index that doesn’t exist
Why are these errors called “exceptions”?
They are unusual cases, not expected during normal program flow.
What is exception handling?
Techniques Java uses to detect, handle, and respond to errors in a controlled way using OOP concepts.
What are the two main types of exceptions in java?
- Unchecked Exceptions: come form the Error class or the RunTimeException class
- Checked Exceptions: are expected errors that the programmer should prepare for. They descend from the Exception class.
What are the types of errors?
- Runtime error
- Syntax error
What is the difference between runtime errors and syntax errors?
- Runtime errors: Happen while the program is running
- Syntax errors: Mistakes in code structure, found before running (during compilation)
If your method might throw a checked exception, you must do one of the following:
- Catch it using a try…catch block inside the method
OR
- Declare it using a throws clause in the method header
What are the two main classes Java uses to handle errors?
Exception and Error (both from Throwable, which extends Object).
What is the Error class?
Represents serious system-level problems (e.g., out of memory). Programmers usually do not handle these.
What is the Exception class?
Represents less serious, recoverable problems (e.g., invalid array index). Programmers can handle these using try, catch, and finally.
What happens when an exception occurs in Java?
Java throws an Exception and displays an error message.
What happens with integer division by zero?
Throws ArithmeticException and causes a crash.
What happens with floating-point division by zero?
Result is Infinity.
How many categories of exceptions does Java recognize?
Over 75, with names like ActivationException, AlreadyBoundException, AWTException, etc.
What is a crash in programming?
The program stops running suddenly due to an error.
What is a stack trace?
A list of method calls that led to the error, shown in the error message.
Should you always handle exceptions?
Not always, but letting a program crash is not ideal, especially for important programs.
How does exception handling improve programs?
Allows catching errors, giving useful messages, and continuing or safely stopping the program.
How did programmers prevent errors before exception handling?
Using if-statements to avoid errors (e.g., checking denominator before division).
What are the limitations of old-school error prevention?
Can’t prevent all errors (e.g., entering a string instead of a number).
What are the advantages of OOP exception handling?
Better error detection, response, and recovery.
What is a fault-tolerant program?
Keeps running even if something goes wrong, possibly with reduced features.
What is a robust program?
Can handle unexpected problems and is reliable under stress or errors.