Java Reference
In-Depth Information
InvalidMediaFormatException
provides a constructor that calls
Excep-
tion
's
public Exception(String message)
constructorwithadetailmes-
sagethatincludestheexpectedandexistingformats.Itiswisetocapturesuchdetailsin
thedetailmessagebecausetheproblemthatledtotheexceptionmightbehardtorepro-
duce.
InvalidMediaFormatException
also provides
getExpectedFormat()
and
getExistingFormat()
methods that return these formats. Perhaps a handler
will present this information in a message to the user. Unlike the detail message, this
messagemightbe
localized
,expressedintheuser'slanguage(French,German,English,
and so on).
Throwing Exceptions
Now that you have created an
InvalidMediaFormatException
class, you can
declarethe
Media
classandbegintocodeits
convert()
method.Theinitialversion
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 3-25
.
Listing 3-25.
Throwing exceptions from the
convert()
method
package media;
import java.io.IOException;
public
final
class Media
{
public static void convert(String srcName, String
dstName)
throws InvalidMediaFormatException, IOException
{
if (srcName == null)
throw new ullPointerException(srcName+" is ull");
if (dstName == 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.