Figure 435. Method printStackTrace() Slide presentation Create comment in forum
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 436. Ascending inheritance ordering Slide presentation Create comment in forum
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 437. Descending inheritance ordering Slide presentation Create comment in forum
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 438. Implementing convert Slide presentation Create comment in forum
/**
 * 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 439. Problem: Silent errors Slide presentation Create comment in forum
  • 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 440. Step 1: Find exception base class Slide presentation Create comment in forum

Figure 441. Step 2: Derive CardinalException Slide presentation Create comment in forum
public class CardinalException
  extends IllegalArgumentException {

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

Figure 442. Step 3: Throwing CardinalException Slide presentation Create comment in forum
/**
 * 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 443. Step 4: Unit test throwing CardinalException Slide presentation Create comment in forum
@Test public void testRegular() {
  Assert.assertEquals("second", Cardinal.convert("two"));
}

@Test(expected = CardinalException.class)
public void testException() {
  Assert.assertEquals("X", Cardinal.convert("four"));
}