general question about try/catch

Discussion in 'ASP .Net' started by JohnE, Aug 31, 2009.

  1. JohnE

    JohnE Guest

    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
     
    JohnE, Aug 31, 2009
    #1
    1. Advertising

  2. JohnE

    JohnE Guest

    "Mark Rae [MVP]" wrote:

    > "JohnE" <> wrote in message
    > news:D...
    >
    > > 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.

    >
    > You're in the wrong newsgroup. For issues specifically related to C#, please
    > post in: microsoft.public.dotnet.languages.csharp
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    >


    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
     
    JohnE, Aug 31, 2009
    #2
    1. Advertising

  3. JohnE

    Hans Kesting Guest

    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
     
    Hans Kesting, Aug 31, 2009
    #3
  4. JohnE

    JohnE Guest

    "Mark Rae [MVP]" wrote:

    > "JohnE" <> wrote in message
    > news:...
    >
    > >>> 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.
    > >>
    > >> You're in the wrong newsgroup. For issues specifically related to C#,
    > >> please
    > >> post in: microsoft.public.dotnet.languages.csharp

    > >
    > > Not wrong group.

    >
    > Yes it is.
    >
    >
    > > I'm using C# in the ASP.Net webapp that I am working on.

    >
    > Irrelevant. Your question relates specifically to the C# language and would
    > this apply to any app you wrote with it, not just ASP.NET...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.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.
     
    JohnE, Aug 31, 2009
    #4
  5. JohnE

    bruce barker Guest

    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)

    JohnE wrote:
    > 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
     
    bruce barker, Aug 31, 2009
    #5
  6. JohnE

    JohnE Guest

    "Mark Rae [MVP]" wrote:

    > "JohnE" <> wrote in message
    > news:...
    >
    > > So, to anyone else who reads this posting can disregard it because it is
    > > in
    > > the wrong group. Thank you for you time.

    >
    >
    > Generally speaking, you get the best responses in the fastest time by
    > posting in the correct newsgroup.
    >
    > I'm actually trying to help you...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    >


    I know you are. Appreciate it. My day didn't start out on a good note but
    is getting better.
    Thanks.
    John
     
    JohnE, Aug 31, 2009
    #6
  7. JohnE

    JohnE Guest

    "Mark Rae [MVP]" wrote:

    > "JohnE" <> wrote in message
    > news:...
    >
    > > So, to anyone else who reads this posting can disregard it because it is
    > > in
    > > the wrong group. Thank you for you time.

    >
    >
    > Generally speaking, you get the best responses in the fastest time by
    > posting in the correct newsgroup.
    >
    > I'm actually trying to help you...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    >


    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
     
    JohnE, Aug 31, 2009
    #7
  8. JohnE

    Ralph Guest

    On Aug 31, 2:20 pm, JohnE <> wrote:
    > "Mark Rae [MVP]" wrote:
    > > "JohnE" <> wrote in message
    > >news:...

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

    >
    > > Generally speaking, you get the best responses in the fastest time by
    > > posting in the correct newsgroup.

    >
    > > I'm actually trying to help you...

    >
    > > --
    > > Mark Rae
    > > ASP.NET MVP
    > >http://www.markrae.net

    >
    > 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.
     
    Ralph, Aug 31, 2009
    #8
  9. JohnE

    Giles Evans Guest

    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "JohnE" <> wrote in message
    > news:...


    >> I'm using C# in the ASP.Net webapp that I am working on.

    >
    > 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.)
     
    Giles Evans, Sep 2, 2009
    #9
  10. JohnE

    Giles Evans Guest


    > 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#"
    >
    >> Does it not equally apply to VB?

    >
    > 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?
     
    Giles Evans, Sep 4, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    Kevin Spencer
    Aug 7, 2003
  2. bienwell
    Replies:
    4
    Views:
    3,869
    bienwell
    May 27, 2005
  3. Matt
    Replies:
    1
    Views:
    414
    Michael Rauscher
    Jun 12, 2004
  4. Matt
    Replies:
    5
    Views:
    427
    Adam Maass
    Jun 28, 2004
  5. xuatla

    question on try/catch

    xuatla, Mar 5, 2005, in forum: C++
    Replies:
    4
    Views:
    1,956
    Andrew Koenig
    Mar 5, 2005
Loading...

Share This Page