473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Return a boolean or raise an exception (from a LogOn method)?

I have a class that has a LogOn method.

I'm trying to think what the advantages are two approaches I have.

a) Having a bool returned containing the success of the LogOn.
b) Raising an exception that should be caught.

I thought that exceptions should not really be raised for "expected"
things and since passing, say, an invalid password to the LogOn routine
is something that is bound to happen sooner or later (and is thus
expected), perhaps it should lead to the LogOn method returning false
rather than true.

Also it affects the client side calling code. Should a client expect an
exception from a LogOn method? If so then the programmer also needs to
be aware that that method is "dangerous" and so needs to put a call to
this method within a try/catch block rather than an if block. But I
guess this should go in the documentation for the class that has the
LogOn method ;-)

Ideas anyone? I've currently done it the option a) way but I'm thinking
I should change it to option b).

Flibble
May 17 '06 #1
6 3698
Mr Fibble,

I am under the belief that exceptions should be thrown in just that,
exceptional cases. The case of a password being invalid not, in my opinion
an exceptional case, it is, as a matter of fact, a business logic issue, one
where your logic fails.

I would completely expect a return value to be passed back to me
indicating that the login failed (you don't have to make it bool, if you
want to return more information).

Now, if you had a user trying to access a protected resource, then that
is cause for exception, I believe.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Mr Flibble" <mr********@flibbyly.wobbly.n.et> wrote in message
news:e4***********@custnews.inweb.co.uk...
I have a class that has a LogOn method.

I'm trying to think what the advantages are two approaches I have.

a) Having a bool returned containing the success of the LogOn.
b) Raising an exception that should be caught.

I thought that exceptions should not really be raised for "expected"
things and since passing, say, an invalid password to the LogOn routine
is something that is bound to happen sooner or later (and is thus
expected), perhaps it should lead to the LogOn method returning false
rather than true.

Also it affects the client side calling code. Should a client expect an
exception from a LogOn method? If so then the programmer also needs to
be aware that that method is "dangerous" and so needs to put a call to
this method within a try/catch block rather than an if block. But I
guess this should go in the documentation for the class that has the
LogOn method ;-)

Ideas anyone? I've currently done it the option a) way but I'm thinking
I should change it to option b).

Flibble

May 17 '06 #2
Hello, Mr!

MF> a) Having a bool returned containing the success of the LogOn.
MF> b) Raising an exception that should be caught.

<skip>

MF> Ideas anyone? I've currently done it the option a) way but I'm thinking
MF> I should change it to option b).

The benefit of exception is that you can shed some light why LogOn failed. OTOH returning simple bool will be of no use.
If you don't want to throw exception then you will have to provide an [out] parameter that will tell why LogOn failed.

I we will search for analogies then...
MS api LogonUser returns BOOL, and if error occurr developer can obtained detailed info via GetLastError()...
However, .NET sec API will throw an exception which Message property will ( probably ) show the cause of error...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
May 17 '06 #3
IF you need more information back from the LogOn method then it should
return a enum with one of the possible results. If even more
information is needed, then a LogOnResponse object should be returned.
Throwing an exception is not an appropriate substitute for either of
those.

May 17 '06 #4


Mr Flibble wrote:
I have a class that has a LogOn method.

I'm trying to think what the advantages are two approaches I have.

a) Having a bool returned containing the success of the LogOn.
b) Raising an exception that should be caught.


Returning a boolean on such a complicated operation where many things
can go wrong is not really that useful.

You could provide a pair of functions:

void LogOn(out LogonResult result, args...);
void LogOn(args...);

where the first one puts the result of the logon into the out parameter
and the second throws if the LogOn fails.

The second method should be easy to write in terms of the first:

void LogOn(args...) {
LogonResult result;
LogOn(out result, args...);
if ( !result.IsSuccess )
throw new LogonFailure(result);
}

--
Helge
May 17 '06 #5
>> void LogOn(out LogonResult result, args...);
where the first one puts the result of the logon into the out parameter


Why have an out parameter? Why not just return the object?

LogonResult LogOn(args);

The one possible problem here is that now both methods have the same
signature, but it also allows the two to be merged:

LogonResult LogOn(args....bool throwOnFailure)
{
LoginResult result= new LogonResult();
/// blah-blah-blah
if (!result.IsSuccess && throwOnFailure)
throw new LogonFailure(result);
return result;

}

May 18 '06 #6


ja**********@gmail.com wrote:
void LogOn(out LogonResult result, args...);
where the first one puts the result of the logon into the out parameter

Why have an out parameter? Why not just return the object?


For two reasons:

1. The signatures on the two methods would become the same, as you saw

2. The caller cannot erroneously ignore the out-paramter, if he wants
"try-and-exception-on-error" he can use the other LogOn, if he wants
more info he can use the one with the out-pararmeter
LogonResult LogOn(args);

The one possible problem here is that now both methods have the same
signature, but it also allows the two to be merged:

LogonResult LogOn(args....bool throwOnFailure)


Yes, that is another possibility.

Maybe it's just personal preference but I don't think:

LogOn(args..., false);

really indicates what's going on.

And, I don't really like that style too much, it is the first step on
the path to:

f(bool doThis, bool doThat, bool doButnotAlways);

which i've just seen too many times.

--
Helge
May 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
47
by: Martin DeMello | last post by:
It seems to be a fairly common pattern for an object-modifying method to return None - however, this is often quite inconvenient. For instance def f(lst1, lst2): g((lst1 + lst2).reverse()) #...
2
by: philjhanna | last post by:
Hi, Does anyone know why I can't add return false with addEventListener in firefox (1.0.6). This demonstrates the problem If return false is added to onmousedown then you can drag over the...
3
by: Eric the half a Bee | last post by:
Hello I am trying to implement a search function within a collection of Employees. I am searching for a specific EmpID number in the collection, and if it is found, I want to return the...
2
by: Raj | last post by:
Hi, When we are sorting the DataGrid Boolean column the grid is becoming redcross. I have my own PPMIPDataGridBoolColumn class inherited from System.Windows.Forms.DataGridBoolColumn. In this...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from...
1
by: aspineux | last post by:
imaplib use exception to report errors, but some problems must be detected by checking the return value ! For example, when trying to append into a mailbox with wrong ACL, imaplib return 'NO',...
270
by: Jordan | last post by:
Hi everyone, I'm a big Python fan who used to be involved semi regularly in comp.lang.python (lots of lurking, occasional posting) but kind of trailed off a bit. I just wrote a frustration...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.