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, Exception)

.NET Framework 1.1

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

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

Parameters

message
The text of the error message.
innerException
A nested exception.

Remarks

The WebException instance is initialized with the Message property set to the value of message and the InnerException property set to the value of innerException. 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). The Status property is initialized to RequestCanceled.

Example

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

[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 specific to the situation. 
             ' and the 'InnerException' which actually lead to this exception.
            Throw New WebException("Unable to locate the Server with 'www.contoso.com' Uri.", e)
        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(connectUri);

                 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 specific to the situation; 
                 //  and the 'InnerException' which actually lead to this exception.
                 throw new WebException("Unable to locate the Server with 'www.contoso.com' Uri.",e);
           }


[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(connectUri);

   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 specific to the situation;
   //  and the 'InnerException' that actually led to this exception.
   throw new WebException(S"Unable to locate the Server with 'www.contoso.com' Uri.", e);
}

[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 | Exception

Show:
© 2015 Microsoft