473,385 Members | 1,461 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.

Exceptions - cost & trade off question

Hi,

In my years as a VB programmer, I have settled into this pattern of creating
collections classes, with an AddNew() method. AddNew() validates the
parameters, instantiates the object, adds it to the collection, and returns
it. The AddNew() method was used to get around the lack of a constructor in
VB classes.

Now I was just about to rewrite this same pattern in C#, when I realized hey
... I've got constructors, and instead of using a collection object, I can
just have a static method to return an IList or ICollection of objects. And
obviously the constructor will replace the AddNew() method ... but what
about invalid constructor parameters?

I've read that Exceptions have extra over head and should be avoided other
than for the "exception" ... So
should I create a static Create() method to my class or go with the
exceptions?

Any comments are appreciated. I'm kind of struggling to get out of the VB /
classic ASP mindset, so forgive my ignorance I'm missing something basic.

Thanks in advance.

John
Jul 21 '05 #1
5 1345

"John" <Pl****@Reply.To.The.Group.com> wrote in message
news:eK*********************@news20.bellglobal.com ...
Hi,

In my years as a VB programmer, I have settled into this pattern of
creating collections classes, with an AddNew() method. AddNew() validates
the parameters, instantiates the object, adds it to the collection, and
returns it. The AddNew() method was used to get around the lack of a
constructor in VB classes.

Now I was just about to rewrite this same pattern in C#, when I realized
hey .. I've got constructors, and instead of using a collection object, I
can just have a static method to return an IList or ICollection of
objects. And obviously the constructor will replace the AddNew() method
... but what about invalid constructor parameters?

I've read that Exceptions have extra over head and should be avoided other
than for the "exception" ... So
should I create a static Create() method to my class or go with the
exceptions?


Yes, exceptions have overhead, but when used properly that overhead doesn't
cause many issues.

First, exceptions shoud only be used in exceptional conditions, such as bad
parameters(to methods or constructors), broken connections, or missing
files. These are things that are generally out of the methods control(some
other piece of code caused them, either the caller or a remote endpoint).
They should not be used as a mechanism to break out of loops or to pass
messages up the call chain. Their overhead is too high to be using them as a
general purpose construct.

In general I wouldn't create a static create method unless I was following
the factory pattern.
Jul 21 '05 #2
.... cut ...
files. These are things that are generally out of the methods control(some
other piece of code caused them, either the caller or a remote endpoint).
They should not be used as a mechanism to break out of loops or to pass
messages up the call chain. Their overhead is too high to be using them as
a general purpose construct.


Do people actually use Exceptions to break out of loops ??? holy crap!!!
It's like a simulated GOTO. LMAO ... you know I've been programming for
10 years and I've never needed a goto statement.

Thanks for clearing that up.
Jul 21 '05 #3
IMO, no problem here. If you have invalid parameters this is a programming
error with nothign you can do you should raise an exception.

What you saw meant rather that you should avoid things like let's say open a
file and catch the exception if the file is not found. You should instead
check for the file existence first.

Patrice

--

"John" <Pl****@Reply.To.The.Group.com> a écrit dans le message de
news:eK*********************@news20.bellglobal.com ...
Hi,

In my years as a VB programmer, I have settled into this pattern of creating collections classes, with an AddNew() method. AddNew() validates the
parameters, instantiates the object, adds it to the collection, and returns it. The AddNew() method was used to get around the lack of a constructor in VB classes.

Now I was just about to rewrite this same pattern in C#, when I realized hey .. I've got constructors, and instead of using a collection object, I can
just have a static method to return an IList or ICollection of objects. And obviously the constructor will replace the AddNew() method ... but what
about invalid constructor parameters?

I've read that Exceptions have extra over head and should be avoided other
than for the "exception" ... So
should I create a static Create() method to my class or go with the
exceptions?

Any comments are appreciated. I'm kind of struggling to get out of the VB / classic ASP mindset, so forgive my ignorance I'm missing something basic.

Thanks in advance.

John

Jul 21 '05 #4

"Patrice" <no****@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
IMO, no problem here. If you have invalid parameters this is a programming
error with nothign you can do you should raise an exception.

What you saw meant rather that you should avoid things like let's say open
a
file and catch the exception if the file is not found. You should instead
check for the file existence first.


Yet, you have to catch the exceptoin, since the file could be removed
between the call to Exists and the attempt to open it. This particular one
just doesn't work as well as one would like it to.


Jul 21 '05 #5

"John" <Pl****@Reply.To.The.Group.com> wrote in message
news:_d*********************@news20.bellglobal.com ...
... cut ...
files. These are things that are generally out of the methods
control(some other piece of code caused them, either the caller or a
remote endpoint). They should not be used as a mechanism to break out of
loops or to pass messages up the call chain. Their overhead is too high
to be using them as a general purpose construct.


Do people actually use Exceptions to break out of loops ??? holy crap!!!
It's like a simulated GOTO. LMAO ... you know I've been programming for
10 years and I've never needed a goto statement.

Thanks for clearing that up.


I've never seen it personally, but I've heard of it. One would hope that its
something that doesn't occur too often.

Goto has its uses, but its really quite rare indeed.
Jul 21 '05 #6

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

Similar topics

5
by: Jonathan Snook | last post by:
Witty subject aside, I have an issue... shouldn't &trade; or it's numerical equivilent ™ be language independent? Such that if the language of the document is set to "fr" it would be the MC instead...
59
by: kk_oop | last post by:
Hi. I wanted to use exceptions to handle error conditions in my code. I think doing that is useful, as it helps to separate "go" paths from error paths. However, a coding guideline has been...
109
by: MSG | last post by:
Michel Bardiaux <michel.bardiaux@peaktime.be> wrote in message news:<G4idnfgZ0ZfCWbrdRVn2jQ@giganews.com>... > Mark Shelor wrote: > > > > > OK, Sidney, I am considering it. I can certainly...
22
by: Drew | last post by:
How do I know which exceptions are thrown by certain methods? For example, reading a file might throw an IO Exception, etc. In Java, the compiler won't even let you compile unless you put your...
9
by: Alvin Bruney [MVP] | last post by:
Exceptions must not be used to control program flow. I intend to show that this statement is flawed. In some instances, exceptions may be used to control program flow in ways that can lead to...
5
by: John | last post by:
Hi, In my years as a VB programmer, I have settled into this pattern of creating collections classes, with an AddNew() method. AddNew() validates the parameters, instantiates the object, adds...
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
7
by: illegal.prime | last post by:
Hi all, I've got a client/server application and just wanted to ensure that this is expected behavior. I recently set the following configuration in Visual Studio: Debug->Exceptions->Break Into...
12
code green
by: code green | last post by:
I need to monitor the change in the cost price of products. The historic cost of every product is stored in a table 'trade' So by comparing the latest two entries per product based on the field...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...

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.