general question about try/catch

J

JohnE

How (and when) do others use try/catch for error handling (in C#)? I'm
trying to grasp when it is needed and when it isn't.
Thanks.
John
 
J

JohnE

Mark Rae said:
You're in the wrong newsgroup. For issues specifically related to C#, please
post in: microsoft.public.dotnet.languages.csharp

Not wrong group. I'm using C# in the ASP.Net webapp that I am working on.
Sorry for misleading. So the inquiry is still good in this group.
John
 
H

Hans Kesting

JohnE expressed precisely :
How (and when) do others use try/catch for error handling (in C#)? I'm
trying to grasp when it is needed and when it isn't.
Thanks.
John

Generally: when you expect some code to throw an exception once in a
while AND you can do something about it (and "log and rethrow" does
count as "doing something").

Hans Kesting
 
J

JohnE

Mark Rae said:
Yes it is.



Irrelevant. Your question relates specifically to the C# language and would
this apply to any app you wrote with it, not just ASP.NET...

Ok, fine. If you say so.

So, to anyone else who reads this posting can disregard it because it is in
the wrong group. Thank you for you time.
 
B

bruce barker

there are three common cases:

1) you can catch and rethrow with additional information, that will help
later.

2) you need to use a finally statement to call dispose

3) the module using the try/catch is fully handling the error, and it
will not be rethrown.

-- bruce (sqlwork.com)
 
J

JohnE

Mark Rae said:
Generally speaking, you get the best responses in the fastest time by
posting in the correct newsgroup.

I'm actually trying to help you...

I know you are. Appreciate it. My day didn't start out on a good note but
is getting better.
Thanks.
John
 
J

JohnE

Mark Rae said:
Generally speaking, you get the best responses in the fastest time by
posting in the correct newsgroup.

I'm actually trying to help you...

Although, sofar, from what I have read and googled, it sounds as if I think
there could be an error, use it. Or even if in doubt whether to use it or
not, use it.
John
 
R

Ralph

Although, sofar, from what I have read and googled, it sounds as if I think
there could be an error, use it.  Or even if in doubt whether to use it or
not, use it.  
John

You can handle errors at the application level also the page level for
asp.net pages.
The question you have to ask when to use try catch is do you want to
recover from the error, or do you want to just notify the user nicely
that the app failed and redirect them somewhere.
Basically can your app proceed with what ever exception was thrown.
I created an httpmodule for errorhandling that writes to an error
queue and writes to the application log then sends the user a friendly
error message. If there is an exception that may be thrown that I
know I can handle and proceed with then I actually put that code in a
try catch.
 
G

Giles Evans

Mark Rae said:
Irrelevant. Your question relates specifically to the C# language and
would this apply to any app you wrote with it, not just ASP.NET...

Wrong! The question applies to any language, *not* specifically C#. Does it
not equally apply to VB?

As to the original question.. A general rule of thumb is whenever your code
relies on anything you didn't write inside your own code, use a try block.
(ie: WebResponse() relies on an external WebSite, and should be enclosed in
a try block.)
 
G

Giles Evans

See the first three words of the part of the OP's post that you quoted...
In
case you missed them, they are "I'm using C#"


Not in the OP's case, because he's using C#, not VB. He's not using VB.NET
or VC++ or Delphi.NET or F# either etc...

LOL!! Irrelevant. The question pertains to any language, irregardless of
the OP's stated preference. We can ignore that he stated it was for an ASP
program but focus on the language choice? As a general programming
question, you were right, that there are more appropriate newsgroups better
suited to answer his question. It was how you directed him that I took as
being rather harsh. If he should have a relevant question, or post, did we
now scare him away?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top