Export (0) Print
Expand All

WebException Constructor (String, WebExceptionStatus)

.NET Framework 1.1

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

[Visual Basic]
Public Sub New( _
   ByVal message As String, _
   ByVal status As WebExceptionStatus _
)
[C#]
public WebException(
   string message,
 WebExceptionStatus status
);
[C++]
public: WebException(
   String* message,
 WebExceptionStatus status
);
[JScript]
public function WebException(
   message : String,
 status : WebExceptionStatus
);

Parameters

message
The text of the error message.
status
One of the WebExceptionStatus values.

Remarks

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 a null reference (Nothing in Visual Basic), the Message property is initialized to a system-supplied message.The InnerException and Response properties are initialized to a null reference (Nothing).

Example

[Visual Basic, C#, C++] The following example throws a WebException by specifying an error message and a WebExceptionStatus.

[Visual Basic] 

        Try
            ' A 'Socket' object has been created.
            Dim httpSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            
            ' The IPaddress of the unknown uri is resolved using the 'Dns.Resolve' method. 
             
            Dim hostEntry As IPHostEntry = Dns.Resolve(connectUri)
            
            Dim serverAddress As IPAddress = hostEntry.AddressList(0)
            Dim endPoint As New IPEndPoint(serverAddress, 80)
            httpSocket.Connect(endPoint)
            Console.WriteLine("Connection created successfully")
            httpSocket.Close()
        Catch e As SocketException
            Console.WriteLine((ControlChars.Cr + "Exception thrown." + ControlChars.Cr + "The Original Message is: " + e.Message))
            ' Throw the 'WebException' object with a message string and message status specific to the situation.
            Throw New WebException("Unable to locate the Server with 'www.contoso.com' Uri.", WebExceptionStatus.NameResolutionFailure)
        End Try


[C#] 

         try
        {
               // A 'Socket' object has been created.
               Socket httpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

              // The IPaddress of the unknown uri is resolved using the 'Dns.Resolve' method. 
          
              IPHostEntry hostEntry = Dns.Resolve("http://www.contoso.com");

              IPAddress serverAddress = hostEntry.AddressList[0];
              IPEndPoint endPoint = new IPEndPoint(serverAddress, 80);
               httpSocket.Connect(endPoint);
              Console.WriteLine("Connection created successfully");
              httpSocket.Close();

           }
        catch(SocketException e)
          {
              Console.WriteLine("\nException thrown.\nThe Original Message is: "+e.Message);
              // Throw the 'WebException' object with a message string and message status specific to the situation.
              throw new WebException("Unable to locate the Server with 'www.contoso.com' Uri.",WebExceptionStatus.NameResolutionFailure);
          }

[C++] 
try {
   // A 'Socket' object has been created.
   Socket* httpSocket = new Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp);

   // The IPaddress of the unknown uri is resolved using the 'Dns::Resolve' method.

   IPHostEntry* hostEntry = Dns::Resolve(S"http://www.contoso.com");

   IPAddress* serverAddress = hostEntry->AddressList[0];
   IPEndPoint* endPoint = new IPEndPoint(serverAddress, 80);
   httpSocket->Connect(endPoint);
   Console::WriteLine(S"Connection created successfully");
   httpSocket->Close();

} catch (SocketException* e) {
   Console::WriteLine(S"\nException thrown.\nThe Original Message is: {0}", e->Message);
   // Throw the 'WebException' object with a message string and message status specific to the situation.
   throw new WebException(S"Unable to locate the Server with 'www.contoso.com' Uri.", WebExceptionStatus::NameResolutionFailure);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

WebException Class | WebException Members | System.Net Namespace | WebException Constructor Overload List

Show:
© 2014 Microsoft