Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is always API correct?

 
santiago martin alfageme
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
santiago martin alfageme
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response

Here is the origin of error:


generating 3 errors about wrong catch:


When I changed in catch block
IOException
for
Exception
, The class compiles OK.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic