nerdexam
Oracle

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

Handling Exceptions

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)
  • A
    58% (19)
  • C
    27% (9)
  • E
    15% (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.

AChecking for ArrayindexoutofBoundsException when iterating through an array to determinewhenCorrect

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.

BChecking for Error and. If necessary, restartingthe program to ensure that users are unawareCorrect

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.

CChecking for FileNotFoundException to inform a user that a filename entered is not valid

Checking for FileNotFoundException and informing the user is a good practice, as it handles an expected, recoverable user input error gracefully.

DChecking for ArrayIndexoutofBoundsExcepcion and ensuring that the program can recover if oneCorrect

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.

EChecking for an IOException and ensuring that the program can recover if one occurs

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

#exception handling best practices#runtime exceptions#checked exceptions#Error class

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice