1Z0-803 · Question #20
Which three are bad practices?
The correct answer is A. Checking for ArrayindexoutofBoundsException when iterating through an array to determinewhen B. Checking for Error and. If necessary, restartingthe program to ensure that users are unaware D. Checking for ArrayIndexoutofBoundsExcepcion and ensuring that the program can recover if one. It is considered bad practice to catch ArrayIndexOutOfBoundsException as a primary means of controlling array iteration or recovering from what is typically a programming error. It is also bad practice to catch general Error types, as these indicate severe, often unrecoverable sy
Question
Which three are bad practices?
Options
- AChecking for ArrayindexoutofBoundsException when iterating through an array to determinewhen
- BChecking for Error and. If necessary, restartingthe program to ensure that users are unaware
- CChecking for FileNotFoundException to inform a user that a filename entered is not valid
- DChecking for ArrayIndexoutofBoundsExcepcion and ensuring that the program can recover if one
- EChecking for an IOException and ensuring that the program can recover if one occurs
How the community answered
(33 responses)- A58% (19)
- C27% (9)
- E15% (5)
Why each option
It is considered bad practice to catch ArrayIndexOutOfBoundsException as a primary means of controlling array iteration or recovering from what is typically a programming error. It is also bad practice to catch general Error types, as these indicate severe, often unrecoverable system issues that should not be silently reset.
Catching ArrayIndexOutOfBoundsException to control array iteration is a bad practice because it indicates a fundamental flaw in loop logic, which should be prevented by explicit boundary checks rather than handled as an exceptional control flow.
Catching Error (e.g., OutOfMemoryError) is a bad practice because these exceptions represent severe, unrecoverable system problems; attempting to silently restart a program after such an error masks critical issues and does not address the root cause.
Checking for FileNotFoundException and informing the user is a good practice, as it handles an expected, recoverable user input error gracefully.
Relying on catching ArrayIndexOutOfBoundsException for program recovery is a bad practice because this exception signals a programmer error and should ideally be prevented by proper bounds checking, rather than being part of a normal error recovery strategy.
Checking for IOException and enabling program recovery is a good practice, as I/O issues are common external problems that robust applications should be prepared to handle and potentially recover from.
Concept tested: Exception handling best practices
Source: https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
Topics
Community Discussion
No community discussion yet for this question.