views:

137

answers:

2

I have a question about handling exception. I have a Winform that uses a webservice proxy on each form for data retrieval and processing. Here is where I really got confused and having a long time deciding which is better.

A. For each call in the web service do a try catch to display the error message and allow the user to re try the process by clicking the button again.

B. Since the error occurred on the web-service and the error was probably because the web service was inaccessible, just make a generic try catch in the WinMain function in the Program.cs and show an error message that web service is inaccessible before the application closes.

The main argument in this is A is more user friendly but needs a lot of try catch code. B is easier to code but just lets the application ends. I am leaning on A but am trying to search the net with options how to lessen the code needed to be written to do this. Any ideas there?

+1  A: 

Encapsulate the webservice call and the try/catch block inside a class =)

mkato
Did you mean create a web service class wrapper and also display the error message in the wrapper?
Nassign
Yes. You don't need to put every call to webservices inside this class, only those who must not stop your application.Do you have a generic webservice client class?If yes, you could put this logic inside this generic webservice client. Doing so, you could set a flag like 'shouldCatchErrorsInWebserviceCall' in the implementation class to tell if it should catch them or not.
mkato
+2  A: 

When you add a web reference, the code generator automatically adds "Async" methods to access the web service.

I would recommend that you use the Async methods rather than the synchronous methods. The nice thing about that is that the EventArgs for the Async methods provide an Error property that you can use to see if the request was successful or not.

 private void CheckWebservice(string data)
 {
      WebService.Server server = new WebService.server();
      server.methodCompleted += server_methodCompleted;
      server.methodAsync(data);
 }

 private void server_methodCompleted(object sender, methodCompletedEventArgs e)
 {
      if (e.Error != null)
           if (MessageBox.Show("Error", "Error", MessageBoxButtons.AbortRetryIgore) == DialogResult.Retry)
           {
                // call method to retry
           }
      else
      {
           if (e.Result == "OK") { // Great! }
      }
 }

If you must use the synchronous methods for some reason, then you could, of course, write a class to encapsulate the methods to call your web service so that you can call it from various places without duplicating the code. Your encapsulation class could do all the error handling and return a result.

 class CallWebService
 {
      public enum Result
      { Unknown, Success, NotAvailable, InvalidData } // etc

      public Call(string data)
      {
           Webservice.Server server = new Webservice.Server();
           string result = string.Empty;
           try
           {
                result = server.getResult(data);
           }
           catch (Exception ex) // replace with appropriate exception class
           {
                return Result.NotAvailable;
           }
           if (result == "OK") return Result.Success
           else return Result.InvalidData;
      }
 }
Chris Thompson
I think I would need my calls to be synchronous with the GUI.
Nassign
If you're going to use the synchronous methods, then you better be using them on a background thread or BackgroundWorker. Running them right on the GUI thread is a great way to cause your application to become unresponsive.
Chris Thompson
oh. that would mean i need to make it multithread. hmm... not that inclined to multithreading
Nassign
The background worker class is REALLY easy to use. Yes, it's multi-threading but it's not complicated at all.
Chris Thompson