Throwing Exceptions

Now that you have created an InvalidMediaFormatException class, you can declare the Media class and begin to code its convert() method. The initial version of this method validates its arguments, and then verifies that the source file's media format agrees with the format implied by its file extension. Check out Listing 4-24.

Listing 4-24. Throwing exceptions from the convert() method public static void convert(String srcName, String dstName) throws InvalidMediaFormatException

throw new NullPointerException(srcName + " is null");

throw new NullPointerException(dstName + " is null");

// Code to access source file and verify that its format matches the // format implied by its file extension.

// Assume that the source file's extension is RM (for Real Media) and // that the file's internal signature suggests that its format is

// Microsoft WAVE.

String expectedFormat = "RM";

String existingFormat = "WAVE";

throw new InvalidMediaFormatException(expectedFormat, existingFormat);

Listing 4-24 demonstrates a throws clause, which consists of reserved word throws followed by a comma-separated list of checked exception class names, and which is appended to a method header. This clause identifies all checked exceptions that are thrown out of the method, and which must be handled by some other method.

Listing 4-24 also demonstrates the throw statement, which consists of reserved word throw followed by an instance of Throwable or a subclass. (You typically instantiate an Exception subclass.) This statement throws the instance to the virtual machine, which then searches for a suitable handler to handle the exception.

The first use of the throw statement is to throw a java.lang.NullPointerException instance when a null reference is passed as the source or destination filename. This unchecked exception is commonly thrown to indicate that a contract has been violated via a passed null reference. For example, you cannot pass null filenames to convert().

The second use of the throw statement is to throw an InvalidMediaFormatException instance when the expected media format does not match the existing format. In the contrived example, the exception is thrown because the expected format is RM and the existing format is WAVE.

Unlike InvalidMediaFormatException, NullPointerException is not listed in convert()'s throws clause because NullPointerException instances are unchecked. They can occur so frequently that it is too big a burden to force the developer to properly handle these exceptions. Instead, the developer should write code that minimizes their occurrences.

NullPointerException is one kind of exception that is thrown when an argument proves to be invalid. The java.lang.IllegalArgumentException class generalizes the illegal argument scenario to include other kinds of illegal arguments. For example, Listing 4-25 throws an IllegalArgumentException instance when a numeric argument is negative.

Listing 4-25. Throwing an IllegalArgumentException instance when x is negative (you can't calculate a negative number's square root)

public static double sqrt(double x) {

throw new IllegalArgumentException(x + " is negative");

// Calculate the square root of x.

There are a few additional items to keep in mind when working with throws clauses and throw statements:

■ You can append a throws clause to a constructor and throw an exception from the constructor when something goes wrong while the constructor is executing. The resulting object will not be created.

■ When an exception is thrown out of an application's main() method, the virtual machine terminates the application and calls the exception's printStackTrace() method to print, to the console, the sequence of nested method calls that was awaiting completion when the exception was thrown.

■ If a superclass method declares a throws clause, the overriding subclass method does not have to declare a throws clause. However, if it does declare a throws clause, the clause must not include the names of exception classes that are not also included in the superclass method's throws clause.

■ A checked exception class name does not need to appear in a throws clause when the name of its superclass appears.

■ The compiler reports an error when a method throws a checked exception and does not also handle the exception or list the exception in its throws clause.

■ Do not include the names of unchecked exception classes in a throws clause. These names are not required because such exceptions should never occur. Furthermore, they only clutter source code, and possibly confuse someone who is trying to understand that code.

■ You can declare a checked exception class name in a method's throws clause without throwing an instance of this class from the method. Perhaps the method has yet to be fully coded.

Was this article helpful?

0 0

Post a comment