Link to home
Start Free TrialLog in
Avatar of cindyli
cindyli

asked on

Should a database function always return boolean?

Dear experts,

I just wonder if we should always define a function which accesses database return boolean? Therefore, the calling object can decide if the the database access is successful.

But, how do we pass the error to the calling function? By catch and try? What is the best "expert" way of doing this? Thanks.

Avatar of Tols
Tols

maybe:

public boolean someMethod() throws SQLException
{
...
}

return value shows succes or failure, exception on error.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cindyli

ASKER

thanks. SO, how do I display a user friendly error message on the User Interface if exception is caught?

...
boolean rval = false;
try
{
   rval = someMethod();
}catch (SQLException e)
{
   //code for notification ex:
   String message = ex.getMessage();  //error message
   JOptionPane.showErrorMessage(..,message,..); //fill in parameters
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


[points to Tols]


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
How would db access fail without an exception being thrown?
sudhakar,

   i share the same view as objects, tols answer is not correct here.

>>return value shows succes or failure, exception on error.

This is only a model.
is 'failure' equals 'error' ?

ERRORS - It could be any error (is there only db access errors possible?) Suppose we expects some data, but there isn't any data - that could be treated like an error. Am I right? It all depends on body of method.

:-)
Whatever the error an exception would be far more useful as it would allow the details of the error to the caller, instead of just a flag saying 'something' went wrong.
Plus what if your method needed to return the results of the database call, this would not be possible if the return status was used for a success flag.
If we only want to get some data from database - You are absolutely right.
I thought that JAVA could be used not only for executing SQL queries but for more complicated operations. ;-)

Those method is generally a model of methods performing some operations (not getter or setter methods).
During those operations (ex. authorization procedures, managing data etc.) may occur such situations that are not an error (ex. authorization failure, not enough data processed etc.).

Methods returning some data are usually submethods of above mehod model. If we want to post here all possible method definitions ...

But generally if we concern only on errors than .. yes, exception is the best way to notify the caller about it.

PS: Didn't You wonder why most of JDBC api methods (that are not for getting something) return boolean values?
And one of the advantages of having exceptions is so methods don't need to return error flags/codes.

> PS: Didn't You wonder why most of JDBC api methods (that are not for getting something) return boolean values?

not for indicating errors they dont :)
>not for indicating errors they dont :)

But what for?
depends on the method but certainly not errors which is what is being disussed here.


Maybe for indicating success or failure of operation (failure of operation is somekind of error, isn't it?)

PS. Methods are not only for indicating errors
My thoughts on Tols' answer is like this


>>public boolean someMethod() throws SQLException

He gave the answer that returns boolean value depends on the situation and at the same time he is throwing exception also which i think is most relevant for handling database functions

returning boolean and throwing execption depends on situation is acceptable . Isn't it?
That's the point!
:-)
That approach breaks down as soon as the method actually needs to return a value.
Guess it comes down to the difference between failure and exception.
Thanks for the chat :)

Sorry one last point, the q asks if it should *always* return a boolean, my answer: no :)