• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception vs error code

 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think an int type. The meaning of its value is agreed upon in the program.
 
Jon Entwistle
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

cheers

Jon
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
ThanX.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic