Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot

...

Code Block
bgColor#FFcccc
import java.io.IOException;
import java.lang.reflect.Field;
import sun.misc.Unsafe;

public class UnsafeCode {
  public static void main( String[] args ) throws SecurityException, NoSuchFieldException,
  IllegalArgumentException, IllegalAccessException {
    Field f = Unsafe.class.getDeclaredField("theUnsafe""theUnsafe");
    field.setAccessible(true);
    Unsafe u =  (Unsafe) field.get(null);
    u.throwException(new IOException(""No need to declare this checked exception""));
  }
}

Noncompliant Code Example

...

Code Block
bgColor#FFcccc
public class BadNewInstance {
  private static Throwable throwable;

  private BadNewInstance() throws Throwable {
    throw throwable;
  }

  public static synchronized void undeclaredThrow(Throwable throwable) {
    // These two should not be passed
    if (throwable instanceof IllegalAccessException || throwable instanceof InstantiationException) {
        throw new IllegalArgumentException(); // Unchecked, no declaration required
    }
       
    BadNewInstance.throwable = throwable;
    try {
      BadNewInstance.class.newInstance();
    } catch (InstantiationException e) { /* dead code */ } 
      catch (IllegalAccessException e) { /* dead code */ } 
      finally { BadNewInstance.throwable = null; }  // Avoid memory leak
  }
}

public class UndeclaredException {
  public static void main(String[] args) {   // No declared checked exceptions
    BadNewInstance.undeclaredThrow(new Exception(""Any checked exception""));
  }
}

Even if the programmer wishes to catch and handle the possible checked exceptions, the compiler refuses to believe that any can be thrown in the particular context. One way to deal with this difficulty is to catch Exception and check whether the possible checked exception is an instance of it else re-throw the exception. This is shown below. The most obvious pitfall is that this technique is easy to bypass whenever an unanticipated checked exception is thrown.

Code Block
public static void main(String[] args) {
  try {	
    BadNewInstance.undeclaredThrow(new IOException(""Any checked exception""));
  } catch(Exception e) { 
    if (e instanceof IOException) {
      System.out.println(""IOException occurred"");
    } else if (e instanceof RuntimeException) {
      throw (RuntimeException) e;
    } else {
      //some other unknown checked exception
    }
  } 
} 

...

Code Block
bgColor#ccccff
// Generic type for a builder used to build any object of type T
public interface Builder&lt;T&gt;Builder<T> {
 public T build();
}

A client can pass a builder to a method and request the creation of an object. A bounded wildcard type should be used to constrain the builder's type parameter. In the code snippet that follows, a US Dollar (USD) is built from coins of different denomination.

Code Block
USD buildCurrency(Builder&lt;Builder<? extends denomination&gt;denomination> currencyBuilder) { /* ... */ }

...

Code Block
bgColor#FFcccc
interface Thr&lt;EXCThr<EXC extends Exception&gt;Exception> {
    void fn() throws EXC;
}

public class UndeclaredGen {
  static void undeclaredThrow() throws RuntimeException {
    @SuppressWarnings(&quot;unchecked&quot;"unchecked")  // Suppresses warnings  
    Thr&lt;RuntimeException&gt;Thr<RuntimeException> thr = (Thr&lt;RuntimeException&gt;Thr<RuntimeException>)(Thr)
      new Thr&lt;IOException&gt;Thr<IOException>() {
        public void fn() throws IOException {
          throw new IOException();
	}
      };
      thr.fn();
    }

  public static void main(String[] args) {
    undeclaredThrow();
  }
}

...

Wiki Markup
\[[JLS 05|AA. Java References#JLS 05]\] Chapter 11: Exceptions
\[[Venners 03|AA. Java References#Venners 03]\] &quot;"Scalability of Checked Exceptions&quot;"
\[[Roubtsov 03|AA. Java References#Roubtsov 03]\]
\[[Schwarz 04|AA. Java References#Schwarz 04]\]
\[[Goetz 04b|AA. Java References#Goetz 04b]\]
\[[Bloch 08|AA. Java References#Bloch 08]\] Item 2: &quot;"Consider a builder when faced with many constructor parameters&quot;"
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 703|http://cwe.mitre.org/data/definitions/703.html] &quot;"Failure to Handle Exceptional Conditions&quot;", [CWE ID 248|http://cwe.mitre.org/data/definitions/248.html] &quot;"Uncaught Exception&quot;"

...

EXC05-J. Use a class dedicated to reporting exceptions&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      13. Exceptional Behavior (EXC)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      EXC07-J. Restore prior object state on method failure