Rookie in Java and first post. I am learning Java following HeadFirstJava 2 edition. On page 444 there is a program showing how to save and recover objects. Program runs OK. But when I try to compile IOException instead Execption in the recovery object try-catch block readObject() method from ObjectInputStream class reports an error. But API says that throws IOError (as writeObject from ObjectOutputStream). With "Execption" goes ok as HeadFirstJava shows. How can I guess the right exception?. Is always API correct?. Thanks in advance
If you want a detailed look at your specific problem, you'll need to post your code (in CODE tags please).
Regarding the correctness of the API documentation, it is partly written by humans, so errors are always possible. However, in general, it is good. And things like lists of which exceptions are thrown by a method are machine-generated, hence very unlikely indeed to be wrong. When you experience a problem with your code, you should definitely suspect your code before an API error.
The source code for the API classes is available, though usually it is in a single JAR, unless you have explicitly unjarred it. Looking at the source code can help you to confirm whether the API documentation is correct.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Exception is a common super class of both IOException and ClassNotFoundException, so if you catch Exception you will catch them both.
To catch them separately, add another catch statement:
You can always use it this way, with some exceptions (no pun intended): 1) You can only catch exceptions that are actually thrown, or RunTimeException and its sub classes (a.k.a. unchecked exceptions) 2) Once you catch an exception, you cannot catch a sub class of that exception below it. E.g. if you catch Exception first, you then cannot catch IOException. Instead, swap them so IOException goes first.
santiago martin alfageme
Joined: Apr 27, 2007
Thanks a lot Rob Now I understand and a moral: I must learn more about reading API. I�ve heard that JavaRanch was quick at answering: But I never realized so quick!