|
|
|
|
Your First Encounter with ExceptionsThe first time you see mention of Java exceptions is
likely to be when you are calling a method supplied in the
Java class library.
However, when you compile your program, you find that it gives you the following compilation error:
What this is telling you is that you can't ignore the fact that the FileReader class constructor may, under some circumstances, throw a FileNotFoundException. Unless you do something, you can't even compile the program. Method SignaturesA class method (or constructor) may specify in its signature that it throws an exception. In the case of the FileReader class constructor, the signature looks like this: public FileReader(String fileName) throws FileNotFoundException A method may specify that it throws more than one exception: public void doIt() throws SomeException, SomeOtherException, YetAnotherException In fact, a method must specify that it throws an
exception if it throws that exception in its body. Java's Catch or Specify RequirementWhen a method calls a method which specifies that it throws an exception, Java requires that the calling method do one of two things:
|
| The page was last updated February 19, 2008 |