This documentation is archived and is not being maintained.

How to: Create an Exception Type That Can be Thrown by Remote Objects

This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the  Windows Communication Foundation (WCF).

You can create your own exception type that can be thrown by a remote object and caught by a remote caller by deriving from the RemotingException class and implementing the ISerializable interface.

To create an exception type that can be thrown by remote objects and caught by remote callers

  1. Define a class that derives from the RemotingException class.

    Public Class RemotableType
       Inherits MarshalByRefObject
          Implements ISerializable
    ' ...
    End Class 'RemotableType
    
  2. Place the SerializableAttribute attribute on the class.

    <Serializable()> Public Class CustomRemotableException
        Inherits RemotingException
        ' ...
    End Class
    
  3. Implement a deserialization constructor that takes a SerializationInfo object and a StreamingContext object as parameters.

    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
            _internalMessage = info.GetValue("_internalMessage", GetType(String))
    End Sub
    

Example

The following code example provides a simple implementation that, if configured, is copied back to the caller when thrown by the remote server object.

<Serializable()> Public Class CustomRemotableException
    Inherits RemotingException
    Implements ISerializable

    Private _internalMessage As String

    Public Sub New()
        _internalMessage = String.Empty
    End Sub

    Public Sub New(ByVal message As String)
        _internalMessage = message
    End Sub

    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        _internalMessage = info.GetValue("_internalMessage", GetType(String))
    End Sub

    Public Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        info.AddValue("_internalMessage", _internalMessage)
    End Sub

    Public Overrides ReadOnly Property Message() As String
        Get
            Return "This is your custom remotable exception returning : """ + _internalMessage + """"
        End Get
    End Property
End Class

See Also

Show: