Exceptions - cost & trade off question

  • Thread starter Thread starter John
  • Start date Start date
J

John

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
 
John said:
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.
 
.... 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.
 
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
 
Patrice said:
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.
 
John said:
... cut ...

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.
 
Back
Top