• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are check-methods with boolean return or with throws Exception equal?

 
Olena Golub
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have one question about the method definitions.
For example, I have defined one method that checks if the record valid for booking.
Let's call it:

There are two possibility to define this method.
first with return boolean value

second: with no return value, but with throwing Exception:


Are this two possibility equals? Or should I make such method with exeptions?
What do you think about it?
Thanks a lot for your help!
Regards,
Olena
[ April 18, 2005: Message edited by: Olena Golub ]
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Olena,

In my assignment I had a method such as your second one (the one that throws the ecxeption). So that seems OK to me

But I would change the name. I would expect a method that starts with "is" to return a boolean. And we don't want to confuse the assessors

Frans.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions should be reserved for times when the requested method cannot preform its main operation due to some reason. For instance,is from the java.lang.Integer Class. Here, the function of the method is to parse a String arguement into a signed decimal integer. If it cannot do this (because the String does not represent an int), then it throws the exception.

In your question, the function of the isRecordValid method to to check if a record is valid or not. It should not throw an exception if the record is not valid because the method itself is correctly carrying out its requested function.

So, IMHO, it is better design to have a boolean return value. I hope that made sense... (My brain is on overload today, so I may not have explained myself well!)
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul is right. Just to add to his point. If for some reason, your function cannot check whether the record is valid or not, then it should throw an exception. So, your function can either
a) return true, means record is valid
b) return false, means record is not valid
c) throw exception, means an error occiured that the function could not handle, hence it does'nt know whther the record is valid or not

So, the correct answer is that it should return boolean AND throw exception.
 
Wei-ju Wu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general the rule is that exceptions should be for exceptional cases, so I agree with Paul and Jayesh. I have the book() function throw an exception too, though. The reason is that booking a record is an exceptional case in my solution and it leaves more possibilities to analyze what went wrong (and I do not like the Win32 API way of returning HRESULTs...)
 
Olena Golub
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I get it!
I am very thankful for your help and comments!
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to this, I completely agree to what all others have said in this thread: exceptions must only be used for exceptional situations.

But since a record that does not exist is a exceptional situation in most of the methods in the interface, one could make one private methods that checks for existance and use that in all other methods. You would then use it like this:



If you merely wanted to know if a record exists, the boolean approach is to be preferred.

Frans.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic