Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: changed intro and CS

Programmers often fall into the trap of suppressing or ignoring checked exceptions. Unless there is a valid reason for ignoring exceptions, such as the client cannot be expected to stage a recovery, it is important to handle them appropriately. Because the exception disrupts

Catching and suppressing exceptions is considered bad practice for several reasons. Exceptions disrupt the expected control flow of the application, care must be taken to ensure that all statements . For example, statements that occur in the try block , before the catch block, execute as expected. Failure to take the actual system state into account when the exception is caught may result in security issues if the application continues to execute as if nothing has happenedafter the statement that caused the exception, do not execute as required. To ensure that the program does not resume with invalid invariants, the catch block should immediately stop control flow from proceeding, instead of ignoring or suppressing the exception. If the program is capable of recovering from the exceptional condition, the statements in the try block that are required to be executed, must be moved outside the try block.

Noncompliant Code Example

...

Note that even though the application reacts to the exception by printing out a stack trace, the application still it proceeds as if the exception was not thrown, that is, the future long term behavior of the application does not change based on the throwing of the exception. Given that the resulting IOException indicates that an operation attempted by the application failed, it is unlikely that the application will be able to operate successfully in the future by assuming that the attempted operation succeeded.

...

This compliant solution attempts to recover from a FileNotFoundException by forcing the user to specify another file when a particular file does not exist in the user-specific directory.

Code Block
bgColor#ccccff

validFlag = false;
do {
  try {
    // If Requestedrequested file does not exist, throws FileNotFoundException
    // If requested file exists, sets a Boolean flag validFlag to true
  } catch(FileNotFoundException e) { 
    // Ask the user for a different filename 
  }
} while(validFlag != true);
// Use the file

The user is allowed to access files in only the user-specific directory so no file system information is leaked in the process (EXC06-J. Do not allow exceptions to transmit sensitive information).

Exceptions

EX1: It is reasonable to ignore handling an exception that occurs within a catch or finally block, such as when closing a FileInputStream object.

...