Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

WebException Constructor (String, WebExceptionStatus)

Initializes a new instance of the WebException class with the specified error message and status.

Namespace:  System.Net
Assembly:  System.Net (in System.Net.dll)

public WebException(
	string message,
	WebExceptionStatus status
)

Parameters

message
Type: System.String
The text of the error message.
status
Type: System.Net.WebExceptionStatus
One of the WebExceptionStatus values.

The WebException instance is initialized with the Message property set to the value of message and the Status property set to the value of status. If message is null, the Message property is initialized to a system-supplied message. The InnerException and Response properties are initialized to null.

The following example throws a WebException by specifying an error message and a WebExceptionStatus.


  public class Example
  {
    static ManualResetEvent clientDone = new ManualResetEvent(false);

    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
		    DnsEndPoint hostEntry = new DnsEndPoint("http://www.contoso.com", 80);

        // Create a socket and connect to the server
        Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);

        socketEventArg.RemoteEndPoint = hostEntry;
        socketEventArg.UserToken = sock;
        sock.ConnectAsync(socketEventArg);
        clientDone.WaitOne();
    }

    static void SocketEventArg_Completed(object sender, SocketAsyncEventArgs e)
    {
         if (e.LastOperation == SocketAsyncOperation.Connect) {
           if (e.SocketError == SocketError.Success)
            {
                // Successfully connected to the server

            }
            else
            {
	   	          // Throw the WebException with no parameters.
		            throw new WebException("Unable to connect to 'www.contoso.com' Uri.",WebExceptionStatus.ConnectFailure);
		        }
		     } 
		     // socket operation not a connect!  
         else
            throw new Exception("Invalid operation completed");
    }
  }


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2015 Microsoft