Nov. 20, 2009, 7:54 a.m.
posted by angryuser
Handling Errors and SOAP FaultsErrors are handled on the client by catching exceptions. Generally speaking, you will catch two types of exceptions: Web exceptions from network errors such as timeouts, and SOAP exceptions, which will wrap SOAP faults. You may remember from Chapter 3 that any exceptions thrown from a .NET server will be sent as SOAP faults. You can also throw SoapExceptions to customize the SOAP fault that is returned. On the client, you can catch these faults, which will be wrapped as SoapExceptions, and then thrown on the client. Instead of creating these objects, you will want to read the information found within them, by catching them. The following snippet shows how you can create a try/catch block around a client request. If you want, you can specifically catch a Soap Exception.
try
{
localhost.Service1 s = new localhost.Service1();
MessageBox.Show( s.Divide( 0, 0).ToString() );
}
catch( SoapException fault )
{
MessageBox.Show( fault.ToString() );
}
catch( Exception ex )
{
MessageBox.Show( ex.ToString() );
}
This results in the creation of a message box that looks something like the one in Figure. Note that the InnerException property of the SoapException is always null. You can't set it on the server and expect the client to contain the correct information. 4. A MessageBox Exception
You also can catch other interesting exceptions, such as WebException, which indicates network errors. For example, if the response is a 404 Not Found HTTP error, you can catch the specific exception of the underlying HttpWebRequest object's exception. The following code shows two catch blocks—one for SoapException and another for WebException:
try
{
localhost.Service1 s = new localhost.Service1();
MessageBox.Show( s.Divide( 0, 0).ToString() );
}
catch( SoapException fault )
{
MessageBox.Show( fault.ToString() );
}
catch( WebException webx )
{
MessageBox.Show( webx.ToString() );
}
This creates a different MessageBox text, as shown in Figure. Figure. A 404 Not Found Exception
|
- Comment

