What would be the purpose of making a user-defined exception serializable?

42 Views Asked by At

What would be the purpose of making a user-defined exception serializable?

I am considering the use of a custom Exception class. But this is new to me, and I found the examples below at Microsoft.

The first exception class is simple, but the second one adds the <Serializable> attribute. What would be the purpose of this?

How To Create User-Defined Exceptions

Public Class EmployeeListNotFoundException
    Inherits Exception

    Public Sub New()
    End Sub

    Public Sub New(message As String)
        MyBase.New(message)
    End Sub

    Public Sub New(message As String, inner As Exception)
        MyBase.New(message, inner)
    End Sub
End Class

How To Create User-Defined Exceptions With Localized Exception Messages

<Serializable>
Public Class StudentNotFoundException
    Inherits Exception

    Public ReadOnly Property StudentName As String

    Public Sub New()
    End Sub

    Public Sub New(message As String)
        MyBase.New(message)
    End Sub

    Public Sub New(message As String, inner As Exception)
        MyBase.New(message, inner)
    End Sub

    Public Sub New(message As String, studentName As String)
        Me.New(message)
        StudentName = studentName
    End Sub
End Class
0

There are 0 best solutions below