File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes is always API correct? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is always API correct?" Watch "is always API correct?" New topic

is always API correct?

santiago martin alfageme

Joined: Apr 27, 2007
Posts: 9
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

Santiago Martin Alfageme
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Are you sure you're not missing some exceptions in the catch?

ObjectInputStream is listed to throw the following exceptions:
- ClassNotFoundException
- InvalidClassException
- StreamCorruptedException
- OptionalDataException
- IOException

While the last 4 are all IOException or a sub class of IOException, you don't need to explicitly catch those. You do have to explicitly catch (or throw) ClassNotFoundException.

How To Ask Questions How To Answer Questions
santiago martin alfageme

Joined: Apr 27, 2007
Posts: 9
Thanks for your response

Here is the origin of error:

generating 3 errors about wrong catch:

When I changed in catch block
, The class compiles OK.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

The error says exactly what I told you.

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
Posts: 9
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!
I agree. Here's the link:
subject: is always API correct?
It's not a secret anymore!