WebException Constructors

Definition

Initializes a new instance of the WebException class.

Overloads

WebException()

Initializes a new instance of the WebException class.

WebException(String)

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

WebException(SerializationInfo, StreamingContext)
Obsolete.

Initializes a new instance of the WebException class from the specified SerializationInfo and StreamingContext instances.

WebException(String, WebExceptionStatus)

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

WebException(String, Exception)

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

WebException(String, Exception, WebExceptionStatus, WebResponse)

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

WebException()

Source:
WebException.cs
Source:
WebException.cs
Source:
WebException.cs

Initializes a new instance of the WebException class.

C#
public WebException();

Examples

The following example throws a default WebException.

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)
  {
  String exp = e.Message;	
  // Throw the WebException with no parameters.
     throw new WebException();
  }

Remarks

The parameterless constructor initializes a new instance of the WebException class. The Message property is initialized to a system-supplied message that describes the error. This message takes into account the current system culture. The InnerException and Response properties are initialized to null. The Status property is initialized to RequestCanceled.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

WebException(String)

Source:
WebException.cs
Source:
WebException.cs
Source:
WebException.cs

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

C#
public WebException(string message);
C#
public WebException(string? message);

Parameters

message
String

The text of the error message.

Examples

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

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.
     throw new WebException("Unable to locate the Server with 'www.contoso.com' Uri.");
  }

Remarks

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

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

WebException(SerializationInfo, StreamingContext)

Source:
WebException.cs
Source:
WebException.cs
Source:
WebException.cs

Caution

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

Initializes a new instance of the WebException class from the specified SerializationInfo and StreamingContext instances.

C#
protected WebException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext);
C#
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected WebException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext);

Parameters

serializationInfo
SerializationInfo

A SerializationInfo that contains the information required to serialize the new WebException.

streamingContext
StreamingContext

A StreamingContext that contains the source of the serialized stream that is associated with the new WebException.

Attributes

Remarks

This constructor implements the ISerializable interface for the WebException class.

See also

Applies to

.NET 9 and other versions
Product Versions (Obsolete)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 (8, 9)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WebException(String, WebExceptionStatus)

Source:
WebException.cs
Source:
WebException.cs
Source:
WebException.cs

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

C#
public WebException(string message, System.Net.WebExceptionStatus status);
C#
public WebException(string? message, System.Net.WebExceptionStatus status);

Parameters

message
String

The text of the error message.

status
WebExceptionStatus

One of the WebExceptionStatus values.

Examples

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

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);
  }

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 null, the Message property is initialized to a system-supplied message. The InnerException and Response properties are initialized to null.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

WebException(String, Exception)

Source:
WebException.cs
Source:
WebException.cs
Source:
WebException.cs

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

C#
public WebException(string message, Exception inner);
C#
public WebException(string? message, Exception? innerException);
C#
public WebException(string message, Exception innerException);

Parameters

message
String

The text of the error message.

innerinnerException
Exception

A nested exception.

Examples

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

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);
    }

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 null, the Message property is initialized to a system-supplied message. The InnerException and Response properties are initialized to null. The Status property is initialized to RequestCanceled.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

WebException(String, Exception, WebExceptionStatus, WebResponse)

Source:
WebException.cs
Source:
WebException.cs
Source:
WebException.cs

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

C#
public WebException(string message, Exception inner, System.Net.WebExceptionStatus status, System.Net.WebResponse response);
C#
public WebException(string? message, Exception? innerException, System.Net.WebExceptionStatus status, System.Net.WebResponse? response);
C#
public WebException(string message, Exception innerException, System.Net.WebExceptionStatus status, System.Net.WebResponse response);

Parameters

message
String

The text of the error message.

innerinnerException
Exception

A nested exception.

status
WebExceptionStatus

One of the WebExceptionStatus values.

response
WebResponse

A WebResponse instance that contains the response from the remote host.

Examples

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

C#
      // Send the data. 
      Encoding ASCII = Encoding.ASCII;
      string requestPage = "GET /nhjj.htm HTTP/1.1\r\nHost: " + connectUri + "\r\nConnection: Close\r\n\r\n";
      Byte[] byteGet = ASCII.GetBytes(requestPage);
      Byte[] recvBytes = new Byte[256];

      // Create an 'IPEndPoint' object.
 
      IPHostEntry hostEntry = Dns.Resolve(connectUri);
      IPAddress serverAddress = hostEntry.AddressList[0];
      IPEndPoint endPoint = new IPEndPoint(serverAddress, 80);

      // Create a 'Socket' object  for sending data.
      Socket connectSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
 
      // Connect to host using 'IPEndPoint' object.

      connectSocket.Connect(endPoint);
 
      // Sent the 'requestPage' text to the host.
      connectSocket.Send(byteGet, byteGet.Length, 0);
 
      // Receive the information sent by the server.
      Int32 bytesReceived = connectSocket.Receive(recvBytes, recvBytes.Length, 0);
      String headerString = ASCII.GetString(recvBytes, 0, bytesReceived);

     // Check whether 'status 404' is there or not in the information sent by server.
     if(headerString.IndexOf("404")!=-1)
     {	 
         bytesReceived = connectSocket.Receive(recvBytes, recvBytes.Length, 0);
         MemoryStream memoryStream = new MemoryStream(recvBytes);
         getStream = (Stream) memoryStream;
 
         // Create a 'WebResponse' object
         WebResponse myWebResponse = (WebResponse) new HttpConnect(getStream);
         Exception myException = new Exception("File Not found");

         // Throw the 'WebException' object with a message string, message status,InnerException and WebResponse
         throw new WebException("The Requested page is not found.",myException,WebExceptionStatus.ProtocolError,myWebResponse);
     }

     connectSocket.Close();

Remarks

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0