Creating Your Own Exception Type


You can create and throw an exception object that is of an existing Java exception type. But creating your own custom exception type can add clarity to your application. A custom exception type can also make testing easier.

As mentioned earlier, you want to propagate exceptions for things that you cannot otherwise control in your application. Prefer any approach where you have a means of preventing an exception from being thrown.

One case where exceptions are more appropriate is when a constructor takes a parameter that must be validated, as in the URL example. You could control validation via a static method call (for example, public static boolean URL.isValid(String url)). But it is reasonable for a constructor to generate an exception as an alternative to forcing clients to first make a static method call.

For example, you may want to reject creation of students with more than three parts to their name, as in this test implemented in StudentTest:


 public void testBadlyFormattedName() {    try {       new Student("a b c d");       fail("expected exception from 4-part name");    }    catch (StudentNameFormatException success) {    } } 

The first step toward realizing this test is creating a new Exception subclass. Before extending Exception directly, you should peruse the Exception types that Sun provides in the Java class library. Look for a similar Exception type on which you might want to base yours. In this case, java.lang.IllegalArgumentException is the most appropriate place to start. Create your StudentNameFormatException class as a subclass of IllegalArgumentException:

 package sis.studentinfo; public class StudentNameFormatException    extends IllegalArgumentException { } 

That's all you need to do to define the exception type. Your new test should now compile and fail, since you do not yet generate the exception in the code. To make the test pass:

 public Student(String fullName) {    this.name = fullName;    credits = 0;    List<String> nameParts = split(fullName);    final int maximumNumberOfNameParts = 3;    if (nameParts.size() > maximumNumberOfNameParts)       throw new StudentNameFormatException();    setName(nameParts); } 

The code demonstrates that you create a new Exception object just like any other object:

 throw new StudentNameFormatException(); 

You throw the exception object using the tHRow keyword:

 throw new StudentNameFormatException(); 

Since your class StudentNameFormatException is a subclass of IllegalArgumentException, and since IllegalArgumentException is a subclass of RuntimeException, you do not need to declare the throws clause on the Student constructor.

Note that there is no test class for StudentNameFormatException! The reason is that there is virtually no code that could break. Yes, you could forget to extend from an Exception superclass, but your code would then not compile.



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