Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: few tweaks

Wiki Markup
            Programs must not catch {{java.lang.NullPointerException}}. A {{NullPointerException}} exception thrown at runtime indicates the existence of an underlying {{null}} pointer dereference that must be fixed in the application code (see rule [EXP11-J. Never dereference null pointers].) Handling the underlying null pointer dereference by catching the {{NullPointerException}} rather than fixing the underlying problem is inappropriate for several reasons. First, _avoiding_ the exception by catching {{NullPointerException}} executesadds significantlysignificant moreperformance slowlyoverhead than simply adding the necessary checks \[[Bloch 2008|AA. Bibliography#Bloch 08]\]. Second, when there are multiple expressions in a {{try}} block that are capable of throwing a {{NullPointerException}}, it is difficult or impossible to determine which expression is responsible for the exception because the {{NullPointerException}} {{catch}} block handles any {{NullPointerException}} thrown from any location in the {{try}} block. Third, programs rarely remain in an expected and usable state after a {{NullPointerException}} has been thrown. Attempts to continue execution after first catching and logging (or worse, suppressing) the exception rarely succeed. 

...

Omitting the null check means that the program fails more quickly than if the program had returned false and let lets an invoking method discover the null value. A function that throws a NullPointerException without a null check must provide a precondition that the argument being passed to it is not null.

...

Code Block
bgColor#ccccff
import java.io.IOException;

public class DivideException {
  public static void main(String[] args) {
    try {
      division(200, 5);
      division(200, 0); // Divide by zero        
    } catch (ArithmeticException ae) { 
      throw new DivideByZeroException();  // DivideByZeroException extends Exception so is checked 
    } catch (IOException ieex) { 
      ExceptionReporter.report(ex);
    }	    
  }

  public static void division(int totalSum, int totalNumber) throws ArithmeticException, IOException  {  
    int average  = totalSum / totalNumber; 
    // Additional operations that may throw IOException...
    System.out.println("Average: "+ average);   	
  }
}

...

Code Block
bgColor#ccccff
class DoSomethingException extends Exception {
  public DoSomethingException(Throwable cause) {
    super( cause);
  }
    
  // other methods
};

private void doSomething() throws DoSomethingException {
  try {
    // code that might throw an Exception
  } catch (Throwable t) {
    throw new DoSomethingException(t);
  }
}

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="843313bb19c4dff4-256e0ac6-44034a43-b9258c19-454dade8b481c7a4e950d84e"><ac:plain-text-body><![CDATA[

[[Cunningham 1995

AA. Bibliography#Cunningham 95]]

 

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9fc9cb5d84a63509-40bc14b5-41d14ddd-b8b08e76-e97e97019ed45f2be418b461"><ac:plain-text-body><![CDATA[

[[Doshi 2003

AA. Bibliography#Doshi 03]]

 

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e288c7e529199151-d4815fa1-436141e6-8a189719-b49d250687cdc7415a7be895"><ac:plain-text-body><![CDATA[

[[Grand 2002

AA. Bibliography#Grand 02]]

Chapter 8, Behavioral patterns, the Null Object

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ccb6d1372cf160ab-3a88c922-41584d63-8f47aea9-660bb1bbb280dc3d00f9efe9"><ac:plain-text-body><![CDATA[

[[Henney 2003

AA. Bibliography#Henney 03]]

 

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="5ae42dbc63379604-c97b3bfa-4b9f4c9d-a1e380a9-066ab7f4aebd71824943facd"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[Chapter 11, Exceptions

http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d892c0202692932e-548223df-4b954a48-a840a3df-5e9855fff2b384eead2437fe"><ac:plain-text-body><![CDATA[

[[J2SE 2011

AA. Bibliography#J2SE 11]]

Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="911e5cd4d65251c6-85702a2c-4b2840af-a374a4ab-ef6b7ffada05f87bd8750a36"><ac:plain-text-body><![CDATA[

[[Muller 2002

AA. Bibliography#Muller 02]]

 

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c5335155bf721101-8479f3a6-45ab4642-b0f492f5-2dfd205a7611485522cf7963"><ac:plain-text-body><![CDATA[

[[Schweisguth 2003

AA. Bibliography#Schweisguth 03]]

 

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="204b3efe352bc3d5-0ee4f67b-4e8d4f2c-bcadbd66-760aa809ca497b54d01ed185"><ac:plain-text-body><![CDATA[

[[Tutorials 2008

AA. Bibliography#tutorials 08]]

[Exceptions

http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html]

]]></ac:plain-text-body></ac:structured-macro>

...