Messages


Most exceptions store an associated message. The message is more often than not for developer consumption. Only sometimes is the message appropriate to present to an end user of an application (although the message might be used as the determining basis for an appropriate end user message). You typically use the message for debugging purposes. Often you will record the message in an application log file to help document what caused a problem.[5]

[5] See the second half of this lesson for a detailed discussion of logging.

The class Exception allows you to create a new exception object with or without a message. You can retrieve the message from an exception using the method getMessage. For an exception object created without a message, getMessage returns null.

You may want to store an error message within a StudentNameFormatException object that provides more information.

 public void testBadlyFormattedName() {    try {       new Student("a b c d");       fail("expected exception from 4-part name");    }    catch (StudentNameFormatException expectedException) {       assertEquals(          "Student name 'a b c d' contains more than 3 parts",          expectedException.getMessage());    } } 

Instead of declaring a temporary variable success to hold the exception object, you now declare the variable name as expectedException. Just catching the exception is no longer enough for success; the message stored in expectedException must also be as expected.

The test will fail, since e.getMessage() returns null by default. You must modify the StudentNameFormatException constructor to take a message parameter. The constructor can then call the corresponding superclass constructor directly.

 package sis.studentinfo; public class StudentNameFormatException    extends IllegalArgumentException {    public StudentNameFormatException(String message) {       super(message);    } } 

The Student constructor can then put together a message and pass it off to the exception constructor.

 public Student(String fullName) {    this.name = fullName;    credits = 0;    List<String> nameParts = split(fullName);    final int maximumNumberOfNameParts = 3;    if (nameParts.size() > maximumNumberOfNameParts) {       String message =          "Student name '" + fullName +          "' contains more than " + maximumNumberOfNameParts +          " parts";       throw new StudentNameFormatException(message);    }    setName(nameParts); } 

The exception message string does represent duplication between the test and code. You may refactor it now if you choose. Later in this lesson, you will learn how to dynamically construct the message using the String method format. I will refactor the code at that time.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net