473,385 Members | 1,752 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,385 software developers and data experts.

When to throw an exception?

Should I throw an exception in the following scenario?

I have a user class. The constructor takes one argument, the sessionid of
the user. This sessionid corresponds to an entry in a database. The
constructor calls the database with the passed in sessionid to retrieve the
user info. If the database call returns no rows, should I throw an exception
or return some kind of error code from the constructor?


Jul 21 '05 #1
6 1377
Larry,
I would recommend a Manager (or whatever name) class that you could call Manager.GetUser(userId). Using this method, you could simply return a User object or null. I would also mark the User constructor as internal based off your description.

Jason Newell, MCAD
Software Engineer
Larry Foulkrod wrote:
Should I throw an exception in the following scenario?

I have a user class. The constructor takes one argument, the sessionid of
the user. This sessionid corresponds to an entry in a database. The
constructor calls the database with the passed in sessionid to retrieve the
user info. If the database call returns no rows, should I throw an exception
or return some kind of error code from the constructor?

Jul 21 '05 #2
Still, should I throw an exception in the constructor of the user class which
can then be caught by the manager class?

"Jason Newell" wrote:
Larry,
I would recommend a Manager (or whatever name) class that you could call Manager.GetUser(userId). Using this method, you could simply return a User object or null. I would also mark the User constructor as internal based off your description.

Jason Newell, MCAD
Software Engineer
Larry Foulkrod wrote:
Should I throw an exception in the following scenario?

I have a user class. The constructor takes one argument, the sessionid of
the user. This sessionid corresponds to an entry in a database. The
constructor calls the database with the passed in sessionid to retrieve the
user info. If the database call returns no rows, should I throw an exception
or return some kind of error code from the constructor?

Jul 21 '05 #3
Larry,
Exceptions are rather expensive. It would be better to have the manager
class query the database. If there is no info, return null. Otherwise create
a class object and fill in the info that was returned.
Bob
"Larry Foulkrod" <lf*******@nfp.com.(nospam)> wrote in message
news:5C**********************************@microsof t.com...
Still, should I throw an exception in the constructor of the user class
which
can then be caught by the manager class?

"Jason Newell" wrote:
Larry,
I would recommend a Manager (or whatever name) class that you could call
Manager.GetUser(userId). Using this method, you could simply return a
User object or null. I would also mark the User constructor as internal
based off your description.

Jason Newell, MCAD
Software Engineer
Larry Foulkrod wrote:
> Should I throw an exception in the following scenario?
>
> I have a user class. The constructor takes one argument, the sessionid
> of
> the user. This sessionid corresponds to an entry in a database. The
> constructor calls the database with the passed in sessionid to retrieve
> the
> user info. If the database call returns no rows, should I throw an
> exception
> or return some kind of error code from the constructor?
>
>
>
>

Jul 21 '05 #4
Echo what Bob said. Sorry for not being clear enough. The User class should not ever get created if the Manager does not find a matching record in the database.

Jason Newell, MCAD
Software Engineer

Bob Milton wrote:
Larry,
Exceptions are rather expensive. It would be better to have the manager
class query the database. If there is no info, return null. Otherwise create
a class object and fill in the info that was returned.
Bob
"Larry Foulkrod" <lf*******@nfp.com.(nospam)> wrote in message
news:5C**********************************@microsof t.com...
Still, should I throw an exception in the constructor of the user class
which
can then be caught by the manager class?

"Jason Newell" wrote:

Larry,
I would recommend a Manager (or whatever name) class that you could call
Manager.GetUser(userId). Using this method, you could simply return a
User object or null. I would also mark the User constructor as internal
based off your description.

Jason Newell, MCAD
Software Engineer
Larry Foulkrod wrote:

Should I throw an exception in the following scenario?

I have a user class. The constructor takes one argument, the sessionid
of
the user. This sessionid corresponds to an entry in a database. The
constructor calls the database with the passed in sessionid to retrieve
the
user info. If the database call returns no rows, should I throw an
exception
or return some kind of error code from the constructor?



Jul 21 '05 #5
Bob Milton <Do********@newsgroup.nospam> wrote:
Exceptions are rather expensive. It would be better to have the manager
class query the database. If there is no info, return null. Otherwise create
a class object and fill in the info that was returned.


I think it really depends on how exceptional it is to ask for something
which isn't there. If it indicates a significant programming error, and
the whole request should be aborted anyway, then there's nothing wrong
with using an exception IMO.

The "exceptions are expensive" idea is a bit of a myth in my view.
They're only going to end up being expensive in a significant way of
you throw many, many thousand exceptions. As a rough indication, my
laptop can throw of the order of 100,000 exceptions in a second. If
you're throwing anything *like* that number, you're using exceptions in
the wrong way in the first place, and have bigger worries than
performance, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
Is this something that could legitimely happens ? If no throw an exception.

In this partiuclar case I would rather expect to have a new line created for
me in the db assuming this is the first time I deal with this partiuclar
session but it all depends on what it's supposed to do...

Patrice

--

"Larry Foulkrod" <lf*******@nfp.com.(nospam)> a écrit dans le message de
news:8B**********************************@microsof t.com...
Should I throw an exception in the following scenario?

I have a user class. The constructor takes one argument, the sessionid of
the user. This sessionid corresponds to an entry in a database. The
constructor calls the database with the passed in sessionid to retrieve the user info. If the database call returns no rows, should I throw an exception or return some kind of error code from the constructor?

Jul 21 '05 #7

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

Similar topics

0
by: Mike Schilling | last post by:
I have some code that calls methods reflectively (the method called and its parameters are determined by text received in a SOAP message, and I construct a map from strings to MethodInfos). The...
6
by: Arjen | last post by:
Hi, I'm reading the enterprise library documentation and there I see the throw statement. try { // run code } catch(Exception ex) {
4
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
3
by: Mr Newbie | last post by:
When you write code, you can generally structure it so that you handle all the errors, so when is it most appropriate to Throw an exception rather than coding for error handling in a more granular...
2
by: Gummy | last post by:
Hello All, I have a webpage that has two dropdown listboxes. Based on what is selected in these dropdown listboxes, it filters a DataGrid . That works fine. In the DataGrid , when I go to edit...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
1
by: Elliot | last post by:
When decrypt the xml, output "Unable to retrieve the decryption key." Can anyone help me solve the problem? I got the code from http://msdn.microsoft.com/en-us/library/ms229746.aspx using...
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.