Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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. The thrown Because the exception disrupts the expected control flow of the application and , care must be taken to ensure that all statements 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 problems issues if the application continues to execute as if nothing has happened.

...

This noncompliant code example adorns the catch block with an ignore comment and forgoes appropriate exception handling.

Code Block
bgColor#FFCCCC
try {
  //...
} catch(IOException ioe) { 
  // Ignore 
}

...

Note that even though the application reacts to the exception by printing out a stack trace, the application still 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 thrown 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.

...

Code Block
bgColor#ccccff
try {
  // Requested file does not exist
} catch(FileNotFoundException e) { 
  // Ask the user for a different filename */ 
}

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.

...