locked
Throw exception but continue on RRS feed

  • Question

  • I have the following in a base request class and I want to continue if the try catch catches an error because I'm going to be logging a flag to the db in some other classes that use this class for other purposes.  I do not know how to continue past the error:

                    // Send the Request
                    requestStream = request.GetRequestStream();
                    requestStream.Write(data, 0, data.Length);
                    requestStream.Close();
    
    
                    // get response
                    try
                    {
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                        using (response)
                        {
                            if (response.StatusCode != HttpStatusCode.OK)
                            {
                                string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
                                throw new ApplicationException(message);
                            }
    
                            responseStream = response.GetResponseStream();
                        }
                    }
                    catch (Exception ex)
                    {
                        _requestError = true;
                        string message = String.Format("Exception during Http Request: {0}", ex);
                        throw new ApplicationException(message);
                    }

    C# Web Developer
    • Moved by David M Morton Monday, August 31, 2009 3:09 PM ASP.NET Question (From:Visual C# General)
    Monday, August 31, 2009 3:00 PM

Answers

All replies

  • Just don't throw from within the exception.  You'll have to call your handler directly within the Catch statement, and don't rethrow the exception. 

    I'd suggest a static class with a static method that performs the logging for you.  Call that class from within the Catch block, and pass the message into the method.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marked as answer by NoEgo Monday, August 31, 2009 3:26 PM
    Monday, August 31, 2009 3:02 PM
  • Thanks...

    We handle logging globally in the global.asax.  I am honestly not real good at logging.  But if I take out the throw, our global.asax should catch and log that for us I'm pretty sure at least currently that's how we have it set up.  If you look at the prior code, we are explicitly throwing an exception.  If I remove mine in the catch, then I assume the global.asax will not pick this up in the Application_Error?
    C# Web Developer
    Monday, August 31, 2009 3:06 PM
  • I don't believe the Global.asax will pick it up if you don't throw.  But if the global.asax does pick it up, then it's too late to carry on with the code.  You can't have your cake and eat it too here.  You have to either handle the error where it happens and move on, or deal with the entire stack unwinding on it's way to global.asax. 

    In the future, please post ASP.NET specific questions to the ASP.NET forums at http://forums.asp.net/. They are off-topic in C# general.  I'm moving this post to the off-topic posts forum. 
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Monday, August 31, 2009 3:08 PM
  • ah, yea, this would be ASP.NET but I'm really asking about simple error handling..how to move past the error and continue code really.

    C# Web Developer
    Monday, August 31, 2009 3:22 PM
  • If this is simple error handling, the answer would be what I suggested in my first post.  

    Once you start talking about wanting the error to bubble up to the global.asax file, you're in ASP.NET-land. 

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Monday, August 31, 2009 3:26 PM
  • yea, I know.  I just didn't know why it was moved.  Could have just said to ask a new question in the ASP.NET forum and kept this in the C# forum.
    C# Web Developer
    Monday, August 31, 2009 3:27 PM