Re: Accessing Data and Try/Catch
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 8 Jan 2007 10:52:54 -0800
Not a bad article, although I disagree with the reasoning surrounding
the first "excuse": lack of documentation. In particular, I call the
Crystal.NET API, which has absolutely horrid documentation, and I can't
afford to release my app and wait for my users to crash it to find out
what kinds of exceptions Crystal can throw, so I use catch (Exception
ex) there.
The rest of the article, however, is sound, if a little jargon-laden: I
think it can be said more simply.
The classic example is OutOfMemoryException. Do you really (ever) want
to catch that? The rule (supposedly) is that you catch exceptions from
which you can recover. If you catch an exception, you should do
something intelligent with it. What can you do if you run out of
memory? Anything? Probably not. So... don't catch an exception that in
fact you can't handle.
Every time you code catch (Exception ex), you're saying, "I can take
intelligent action to recover from _any_ possible exception here."
That's a big claim, and almost always not true.
Mark R. Dawson wrote:
Hi Jake,
Regarding catching exceptions, is it not a good idea to use
catch (Exception ex)?
Here is a good article on why it is not a good idea to catch a general
exception: http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx
Mark
--
http://www.markdawson.org
http://themightycoder.spaces.live.com
"Jake K" wrote:
Thanks a lot. Regarding catching exceptions, is it not a good idea to use
catch (Exception ex)? I figure there could be a few common errors that may
occur in connecting to a db server and executing some tsql. How is it best
to trap db specific errors if not catch (Exception ex) which will catch all
exceptions.
"Bruce Wood" <brucewood@xxxxxxxxxx> wrote in message
news:1168227459.242850.6470@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Jake K wrote:
Thanks for the reply. So any method called from a static method must
also
be static?
A static method can call other static methods directly, as you saw. It
can also call instance (non-static) methods if it has a class instance
on which to call them. "static" means "associated with the class as a
whole, not with a particular instance." In order to call an instance
(non-static) method, you need to create an instance first using the
"new" operation.
By the way, the particular example code doesn't follow best practices.
You'll more often see Main methods creating a class instance and then
calling methods on that, rather than just making everything static. As
well, catching Exception is a no-no: one should catch specific
exception types, rather than create blanket catch-all. I think that the
code on that page is just showing you how to make the database calls,
not how to program C# correctly.
.
- References:
- Accessing Data and Try/Catch
- From: Jake K
- Re: Accessing Data and Try/Catch
- From: Davey
- Re: Accessing Data and Try/Catch
- From: Jake K
- Re: Accessing Data and Try/Catch
- From: Bruce Wood
- Re: Accessing Data and Try/Catch
- From: Jake K
- Accessing Data and Try/Catch
- Prev by Date: Re: Serves me right for trying something new!
- Next by Date: Re: SqlConnection - Reset
- Previous by thread: Re: Accessing Data and Try/Catch
- Next by thread: Re: Accessing Data and Try/Catch
- Index(es):
Relevant Pages
|