Figure 518. Method printStackTrace() Slide presentation
package exceptionhandling;
public class StackTrace {
  public static void main(
       String[] args){
    a();
  }
  static void a() { b();}
  static void b() { c();}
  static void c() {
    String s = null;
    s.length();
  }
}
Exception in thread "main"
   java.lang.NullPointerException
 at ex.Trace.c(Trace.java:10)
 at ex.Trace.b(Trace.java:7)
 at ex.Trace.a(Trace.java:6)
 at ex.Trace.main(Trace.java:4)

Figure 519. Ascending inheritance ordering Slide presentation
try {
  FileInputStream f = new FileInputStream(
     new File("test.txt"));
} catch(final FileNotFoundException e) {
  System.err.println( "File not found");
} catch (final IOException e) {
  System.err.println( "IO error");
} catch(final Exception e) {
  System.err.println("General error");
}
Ascending inheritance ordering

Figure 520. Descending inheritance ordering Slide presentation
try {
  FileInputStream f = new FileInputStream(
     new File("test.txt"));
} catch(Exception e) {
      System.err.println("General error");
} catch (IOException e) {
      System.err.println( "IO error");
} catch(FileNotFoundException e) {
      System.err.println("File not found");
}
Descending inheritance ordering

Figure 521. Implementing convert Slide presentation
/* Translate {"one", "two", "three"} to {"first", "second", "third"}
 * @param input The input String to be translated.
 * @return See above explanation. */
static public String convert(final String input) {
 switch (input) {
   case "one": return "first";
   case "two": return "second";
   case "three": return "third";
  default: return "no idea for " + input;
  }
}

Figure 522. Problem: Silent errors Slide presentation
  • Return false result, application continues.

  • Solution: Throw an exception. Steps:

    1. Find a suitable exception base class.

    2. Derive a corresponding exception class

    3. Throw the exception accordingly.

    4. Test correct behaviour.


Figure 523. Step 1: Find exception base class Slide presentation

Figure 524. Step 2: Derive CardinalException Slide presentation
public class CardinalException
  extends IllegalArgumentException {

  public CardinalException(final String msg) {
    super(msg);
  }
}

Figure 525. Step 3: Throwing CardinalException Slide presentation
/**
 * Translate {"one", "two", "three"} to {"first", "second", "third"}
 * @param input The input String to be translated.
 * @return See above explanation.
 * @throws CardinalException If input not from list.
 */
static public String convert(final String input)
  throws CardinalException {

  switch (input) {
    case "one": return "first";
    case "two": return "second";
    case "three": return "third";
  }
  throw new CardinalException(
            "Sorry, no translation for '" + input + "' on offer");
}

Figure 526. Step 4: Unit test throwing CardinalException Slide presentation
@Test public void testRegular() {
  Assert.assertEquals("second", Cardinal.convert("two"));
}

@Test(expected = CardinalException.class)
public void testException() {
  Cardinal.convert("four"); // No assert...() required
}