Re: Accessing Data and Try/Catch



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.





.



Relevant Pages

  • Re: a criticism of java
    ... Documentation should be in English rather than java code that wasn't ... (* I personally wouldn't document all kinds of unchecked exceptions. ... Cerainly not programmer errors. ... In the rare event that the user will deal with an unchecked exception ...
    (comp.lang.java.programmer)
  • Re: jambands.com letter to the editor
    ... >>> copying, period, without permission. ... with the exception of brief excerpts in critical ... > have made a statement here that is contrary to the documentation that I have ... prohibited from making photocopies, such as the "documentation" you have ...
    (rec.music.gdead)
  • Re: Source code documentation
    ... An error occured while trying to build the documentation. ... Exception: NDoc.Core.DocumenterException ... Please verify that the HTML Help Workshop has been installed. ... bei NDoc.Documenter.Msdn.MsdnDocumenter.Buildin C:\Dokumente und ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problems Handling Errors Correctly
    ... remember the web site off hand but Google "SandCastle help generator" and you'll find a utility that turns the xml file generated from the code comments into MSDN style documentation ... Every once in a while you'll get an exception that's thrown from deep in the stack that doesn't seem intuitve, but for me it's usually when I'm dealing with remoting or xml stuff. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I get the equivalent status (DEC ADA IMPORT_EXCEPTION)
    ... But you asked about examining subordinate chained messages, ... The documentation describes the Condition ... >> And then use the imported exception values for the documented codes ... > rather cumbersome mapping of exceptions back to status ...
    (comp.os.vms)