Skip to content

Advice on Exception Handling.

Ed

Asked Feb 12, 2009, 11:41 AM EST8 answers
Share
LinkedInX
Hi All,

I just recently took some extensive training on my own learning C# and WebServices. I'm an experienced programmer but relatively new to OO and C#. As a result of that training, I built a Library Management System. This system uses a web service and both an ASP.NET application and a Windows Client.

I'm still trying to get some things figured out, so am posting for advice on the best way to handle certain issues with regards to this LMS, starting with exception handling. In one scenario, I have an Add Book function that reads for the existance of a book, and if it doesn't exist, creates a new ID for it. The problem is, the exception the client is handling is a SoapException, with a RowNotInTableException embedded in it. In order to trap the error, I am handling it like this:
==========
catch (SoapException error) {
  if (error.Message.Contains("Book could not be found.")) {
    AddMode();
    OpenTitle();
  }
  else
    throw error;
}
==========

I know this is not the best way to handle this, but I've also tried handling it like this:
==========
  catch (SoapException error) {
  if (error.InnerException is RowNotInTableException) {
    AddMode();
    OpenTitle();
  }
  else
    throw error;
}
==========

This fails and tells me the exception is unhandled. So, it is hitting the throw error statement in my catch block.

So, what advice does anyone have on the best way to handle this issue? Any and all clues welcome.

Ed.

8 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    I have a question are you developping a web application or a desktop application?
    0
  • 0
    Both, actually. I have an ASP.NET version of the application, and an Window's client version of the app. Both use a web service to access the database.

    Why do you ask?

    Ed.
    0
  • Bechir Bejaoui
    0
    Well, then you can simply program either the Application_error or the Page_error events to transfer the user to  a customized error page say a html page that you build by your self, I give you an example near your situation

    protected void Application_Error(Object sender, EventArgs e)
            {

                if (Server.GetLastError().InnerException != null)
                {
                    Server.Transfer(/*the given url to the error customized page*/);
                 }
    }

    Or you can use Page_error instead
    You can programm that by adding  global.asax to your application. I advise you to digg into details with how to use the global.asax as an experienced programmer you will learn faster how to administer a web application through configuring global asax.

    Or I propose another solution which is sure, becasue tested it. Open the configuration file of your application and add this within the
    <Configuration>
    <System.web>
    ........
    </System.web>
    </Configuration>

    <customErrors mode="On" defaultRedirect="~/AppError.aspx">
                <error statusCode="404/*for example*/" redirect="AppError404.aspx"/>
    </customErrors>
    0
  • Mahesh Chand
    0

    What happens when you get a non-soap error? Sometimes its advised to use generic error handling and nested specific error handling.

    catch (Exception exp)
    {
      catch (SoapException error)
      {

                // thow this

      }

      // You do not have to throw this
    }

    OR you can also do this:

    catch (Exception exp  || SoapException error)
    {
    }

    0
  • Bechir Bejaoui
    0
    But the problem Mahesh That he has a doubt(if I well usnderstood the issue)  that another exception or even error is happening neither the soap of the file exception is happening becasue the error is raisen inspite of adding his catch block and it is out of the capacity of this cath block. This is the problem.
    0
  • 0
    It's not that I doubt that the exception is being handled; using the technique I am, it is being handled. I'm simply not certain that this is the _best way_ to handle the issue and I'm puzzled why the "error.InnerException is RowNotInTableException" doesn't work. Which is why I am asking "is there a better way to do this?"

    Put simply: I'm trapping the soap exception as I should, but in this case, I only want to take action on the RowNotInTableException that is inside and display an error for the others. So, I'm wondering how to act only under those circumstances.

    Ed.
    0
  • Mahesh Chand
    0
    Sorry guys. I didn't read details but as I showed in my psedo sample, using these two techniques, you can trap any error type and manage the way you want.
    0
  • 0
    Hey guys..
    How did this ended?
    Is there no better way on handling exceptions inside SoapExceptions!? :-(
    []s
    0