exception handling Flashcards

(53 cards)

1
Q

What is an exception?

A

An exception is an error or unexpected situation that happens while the program is running.

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

What are the common causes of exception?

A
  • User enters the wrong data type
  • Dividing a number by zero
  • Accessing an array index that doesn’t exist
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why are these errors called “exceptions”?

A

They are unusual cases, not expected during normal program flow.

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

What is exception handling?

A

Techniques Java uses to detect, handle, and respond to errors in a controlled way using OOP concepts.

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

What are the two main types of exceptions in java?

A
  1. Unchecked Exceptions: come form the Error class or the RunTimeException class
  2. Checked Exceptions: are expected errors that the programmer should prepare for. They descend from the Exception class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the types of errors?

A
  1. Runtime error
  2. Syntax error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between runtime errors and syntax errors?

A
  • Runtime errors: Happen while the program is running
  • Syntax errors: Mistakes in code structure, found before running (during compilation)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

If your method might throw a checked exception, you must do one of the following:

A
  • Catch it using a try…catch block inside the method

OR

  • Declare it using a throws clause in the method header
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the two main classes Java uses to handle errors?

A

Exception and Error (both from Throwable, which extends Object).

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

What is the Error class?

A

Represents serious system-level problems (e.g., out of memory). Programmers usually do not handle these.

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

What is the Exception class?

A

Represents less serious, recoverable problems (e.g., invalid array index). Programmers can handle these using try, catch, and finally.

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

What happens when an exception occurs in Java?

A

Java throws an Exception and displays an error message.

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

What happens with integer division by zero?

A

Throws ArithmeticException and causes a crash.

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

What happens with floating-point division by zero?

A

Result is Infinity.

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

How many categories of exceptions does Java recognize?

A

Over 75, with names like ActivationException, AlreadyBoundException, AWTException, etc.

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

What is a crash in programming?

A

The program stops running suddenly due to an error.

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

What is a stack trace?

A

A list of method calls that led to the error, shown in the error message.

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

Should you always handle exceptions?

A

Not always, but letting a program crash is not ideal, especially for important programs.

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

How does exception handling improve programs?

A

Allows catching errors, giving useful messages, and continuing or safely stopping the program.

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

How did programmers prevent errors before exception handling?

A

Using if-statements to avoid errors (e.g., checking denominator before division).

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

What are the limitations of old-school error prevention?

A

Can’t prevent all errors (e.g., entering a string instead of a number).

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

What are the advantages of OOP exception handling?

A

Better error detection, response, and recovery.

23
Q

What is a fault-tolerant program?

A

Keeps running even if something goes wrong, possibly with reduced features.

24
Q

What is a robust program?

A

Can handle unexpected problems and is reliable under stress or errors.

25
How does exception handling help?
Makes programs safer, smarter, and more user-friendly.
26
What happens when an exception is thrown in a try block?
Java looks for a matching catch block and executes it if found.
27
What does the getMessage() method do?
Retrieves Java’s built-in message about the exception.
28
Can you fix problems in the catch block?
Yes, you can add code to fix the problem (e.g., change division by 0 to division by 1).
29
How can try blocks help with user input?
Prevent crashes from incorrect input (e.g., entering a letter instead of a number).
30
What happens if an exception occurs during input?
The program can catch the exception and handle it, allowing the user to continue.
31
How can you allow users to continue entering data after an exception?
By handling each input separately and using nextLine() to clear the input buffer.
32
What happens if you declare a variable inside a try block?
It is local to that block and not accessible outside.
33
What problem occurs if a variable is only initialized inside a try block?
If an exception occurs, the variable may remain uninitialized, causing a compile error.
34
How can you ensure a variable always has a value?
- Assign a default value before the try block - Assign a value in the catch block
35
Can you catch more than one type of exception in a try block?
Yes, by using multiple catch blocks.
36
What happens if multiple exceptions could occur in a try block?
Only the first exception is caught; the rest of the try block is skipped.
37
What is upcasting in exception handling?
Handling multiple specific exceptions with a single general Exception catch block.
38
What is the best practice for multiple catch blocks?
Place specific exceptions first, then the general one last.
39
What is a finally block?
Code that always runs, whether or not an exception occurs, usually for cleanup tasks.
40
Where is the finally block placed?
After the try and catch blocks.
41
Why use finally?
Ensures important code (like closing files or releasing resources) always runs.
42
When might code after catch not run?
- No matching catch block (program crashes) - System.exit() is called in try or catch
43
Does finally always run?
Yes, except when System.exit() is called.
44
How do you specify which exceptions a method can throw?
Using the 'throws' keyword in the method signature.
45
Can you create your own exception classes in Java?
Yes, by extending the Exception class.
46
What is Exception Specififcation?
Refers to the way a Java method declares which exceptions it might throw during its execution.
47
What happens if the program tries to divide a number by zero?
An ArithmeticException is thrown.
48
What happens if the program accesses an array index that doesn’t exist?
ArrayIndexOutOfBoundsException is thrown.
49
What is the purpose of the try block?
To enclose code that might throw an exception.
50
What is the purpose of the catch block?
To handle an exception that was thrown in the try block.
51
If a user enters a letter instead of a number when the code expects an integer, what happens?
An InputMismatchException is thrown.
52
throws Doesn’t Mean an Error Will Happen
- Adding throws ExceptionType doesn’t mean the method will always throw an error. - It only warns other programmers: “This method might throw an exception.”
53
To Properly Use a Method, Know These 3 Things:
1.The return type 2. The types and number of arguments it accepts 3.The exceptions it might throw