Jam*_*oll 12 vb.net exception-handling exception custom-errors custom-error-handling
我有一个自定义异常类,如下所示:
<Serializable>
Public Class SamException
Inherits Exception
Public Sub New()
' Add other code for custom properties here.
End Sub
Public Property OfferBugSend As Boolean = True
Public Sub New(ByVal message As String)
MyBase.New(message)
' Add other code for custom properties here.
End Sub
Public Sub New(ByVal message As String, ByVal inner As Exception)
MyBase.New(message, inner)
' Add other code for custom properties here.
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
我在AJAX响应的返回中使用它来处理某些情况.
在我的AJAX响应的错误函数中,我确定错误属于我的自定义类型,如下所示:
....
ajax code....
.error = function (xhr, text, message) {
var parsed = JSON.parse(xhr.responseText);
var isSamCustomError = (parsed.ExceptionType).toLowerCase().indexOf('samexception') >= 0;
.... etc....
Run Code Online (Sandbox Code Playgroud)
如果错误属于我的自定义类型,这允许我将特定响应回发给客户端.
但是......我似乎无法将额外的属性发布OfferBugSend给客户端,以便AJAX代码以不同的方式处理这种情况.
console.log("OfferBugSend: " + parsed.OfferBugSend)
Run Code Online (Sandbox Code Playgroud)
显示未定义,如果我检查响应,这是因为xhr.responseText只包含属性:
ExceptionType
Message
StackTrace
Run Code Online (Sandbox Code Playgroud)
这些属性来自基类,Exception但它没有传递我的自定义类属性...
我怎么能做到这一点?
Exception 类是可序列化的,但它包含一个 IDictionary 并实现 ISerializable,这需要做更多的工作来序列化自定义异常类。
处理此问题的更简单方法是利用Exception 类的数据集合,如下所示:
Public Property OfferBugSend As Boolean
Get
Return Data("OfferBugSend")
End Get
Set(value As Boolean)
Data("OfferBugSend") = value
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
另一种方法是确保派生类也实现ISerialized接口,这涉及提供序列化构造函数并重写 GetObjectData()。请参阅另一个答案(在 C# 中)作为该方法的基准。
| 归档时间: |
|
| 查看次数: |
132 次 |
| 最近记录: |