Java Reference
In-Depth Information
catch (IOException ioe)
The call to Media 's convert() method in Listing 3-26 is placed in a try block
because this method is capable of throwing an instance of the checked InvalidMe-
diaFormatException , IOException , or FileNotFoundException
class—checked exceptions must be handled or be declared to be thrown via a throws
clause that is appended to the method.
The catch (InvalidMediaFormatException imfe) block's statements
are designed to provide a descriptive error message to the user. A more sophisticated
application would localize these names so that the user could read the message in the
user's language. The developer-oriented detail message is not output because it is not
necessary in this trivial application.
Note Adeveloper-orienteddetailmessageistypicallynotlocalized.Instead,itisex-
pressed in the developer's language. Users should ever see detail messages.
Although not thrown, a catch block for IOException is required because this
checked exception type appears in convert() 's throws clause. Because the catch
(IOException ioe) blockcanalsohandlethrown FileNotFoundException
instances (because FileNotFoundException subclasses IOException ), the
catch (FileNotFoundException fnfe) block isn't necessary at this point,
forreadingorcreatedforwriting(whichwillbeaddressedonce convert() isrefact-
ored to include file code).
Assumingthatthecurrentdirectorycontains Listing3-26 anda media subdirectory
containing and , com-
pile this listing ( javac ), which also compiles media 's source
files,andruntheapplication,asin java Converter A B . Converter responds
by presenting the following output:
Unable to convert A to B
Expecting A to conform to RM format.
However, A conformed to WAVE format.
Search WWH ::

Custom Search