File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes exception vs error code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "exception vs error code" Watch "exception vs error code" New topic

exception vs error code

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Hi I prepare my self for the dev. exam and I have a small dilemma the "error code".
In Katty & Bert on chapter 12 I found :

"Do Not Return Error Codes! This is Java. This is OO. If you really need to indicate an exceptional condition, use an Exception! If you really want to annoy an assessor, use error codes as return values from some of your methods. Even one method might do the trick."

Somethig similar I found in M.Habibi "The sun certified java developer exam ..." in chapter 2 : "Checked exception should be uses to report expected events ...."

What is a error code ?
In the java api (collerction by example) all the data manipulation method rerun a boolean (Inidcates the success/failure of the operation), in the
M.Habibi book the same stuff(public synchronized boolean addDVD(DVD dvd) throws IOException{ ... ) is this boolean a error code or not ?

Andy Zhu
Ranch Hand

Joined: May 26, 2004
Posts: 145
I think an int type. The meaning of its value is agreed upon in the program.

--------<br />Andy Zhu<br />scjp 1.4<br />scjd 1.4<br />SAS Certified Programmer 9.0
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118

The error codes refered to are ways used in languages such as C to determine an exit condition of a procedure when something unexpected has happened - a non zero value where each value indicates what happened (e.g. errno value 5 means an IO error has occurred). You should not do this in your code - we are lucky enough to have exceptions and we would rightly be marked down for not using them when appropriate.



Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

I agree about the "C" error code style.That is logic.
In the other words when a method tries to accomplish a task, olny two possible scenario.It accomplish the task or not, and this can be checked with the boolean retun value.If an expected (or unexpected) event happens during the method task an exception(checked or unchecked) is raised.
Now I have a clear overview.
I agree. Here's the link:
subject: exception vs error code
It's not a secret anymore!