This site is the archived OWASP Foundation Wiki and is no longer accepting Account Requests.
To view the new OWASP Foundation website, please visit https://owasp.org

Difference between revisions of "Catch NullPointerException"

From OWASP
Jump to: navigation, search
Line 1: Line 1:
 
{{Template:Vulnerability}}
 
{{Template:Vulnerability}}
 +
{{Template:Fortify}}
 +
 +
==Abstract==
 +
 +
It is generally a bad practice to catch NullPointerException.
  
 
==Description==
 
==Description==
 +
 +
Programmers typically catch NullPointerException under three circumstances:
 +
 +
# The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem.
 +
# The program explicitly throws a NullPointerException to signal an error condition.
 +
# The code is part of a test harness that supplies unexpected input to the classes under test.
 +
Of these three circumstances, only the last is acceptable.
  
 
==Examples ==
 
==Examples ==
 +
 +
The following code mistakenly catches a NullPointerException.
 +
 +
<pre>
 +
  try {
 +
mysteryMethod();
 +
  }
 +
  catch (NullPointerException npe) {
 +
  }
 +
</pre>
  
 
==Related Threats==
 
==Related Threats==
Line 16: Line 38:
  
 
==Categories==
 
==Categories==
 
{{Template:Stub}}
 
  
 
[[Category:Error Handling Vulnerability]]
 
[[Category:Error Handling Vulnerability]]

Revision as of 17:03, 19 July 2006

This is a Vulnerability. To view all vulnerabilities, please see the Vulnerability Category page.

This article includes content generously donated to OWASP by MicroFocus Logo.png

Abstract

It is generally a bad practice to catch NullPointerException.

Description

Programmers typically catch NullPointerException under three circumstances:

  1. The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem.
  2. The program explicitly throws a NullPointerException to signal an error condition.
  3. The code is part of a test harness that supplies unexpected input to the classes under test.

Of these three circumstances, only the last is acceptable.

Examples

The following code mistakenly catches a NullPointerException.

	  try {
		mysteryMethod();
	  }
	  catch (NullPointerException npe) {
	  }

Related Threats

Related Attacks

Related Vulnerabilities

Related Countermeasures

Category:Error Handling

Categories