【发布时间】:2011-04-19 06:34:23
【问题描述】:
我的文件中有以下代码:
在类 Customer.Page:
Try
If Not SiteContent.CurrentUser(False) Is Nothing Then
If Not SiteContent.CurrentUser(False).IsAdministrator OrElse SiteVariables.CustomerMode Then
SiteContent.PageViewManager.Create(New List(Of Control))
End If
Else
SiteContent.PageViewManager.Create(New List(Of Control))
End If
Catch ex As Heritage.Web.Content.Items.Exceptions.ExceptionGroup
My.Response.Write(ex.Message & "<br />" & ex.StackTrace & "<br />")
End Try
类项目
Public Overridable Sub CheckValidity()
'If the item is recycled then return true'
If IsRecycled() Then
Exit Sub
End If
'ExceptionGroup to store all exceptions which are thrown due to invalid data.'
Dim ExceptionGroup As New Exceptions.ExceptionGroup
Try
'Checks if the item already exists'
Exists()
Catch ex As Exception
'Add any exception as a result of this function to the ExceptionGroup'
ExceptionGroup.AddException(ex)
End Try
'Check each attribute - add any exception which occurs as a result of validating their values to the ExceptionGroup'
For Each Attribute As Items.Attribute In GetAttributes
If TypeOf Attribute Is StringAttribute Then
Dim StringAttribute As StringAttribute = Attribute
Try
If Not StringAttribute.Validate(StringAttribute.Value) Then Throw New Exceptions.ItemExceptions.RequiredFieldException(StringAttribute.Name)
Catch ex As Exception
ExceptionGroup.AddException(ex)
End Try
ElseIf TypeOf Attribute Is IntegerAttribute Then
Dim IntegerAttribute As IntegerAttribute = Attribute
Try
If Not IntegerAttribute.Validate(IntegerAttribute.Value) Then Throw New Exceptions.ItemExceptions.RequiredFieldException(IntegerAttribute.Name)
Catch ex As Exception
ExceptionGroup.AddException(ex)
End Try
ElseIf TypeOf Attribute Is DateTimeAttribute Then
Dim DateTimeAttribute As DateTimeAttribute = Attribute
Try
If Not DateTimeAttribute.Validate(DateTimeAttribute.Value) Then Throw New Exceptions.ItemExceptions.InvalidFormatException(DateTimeAttribute.Name)
Catch ex As Exception
ExceptionGroup.AddException(ex)
End Try
End If
Next
'Rollback the transaction if the ExceptionGroup contains any Exceptions'
If ExceptionGroup.Exceptions.Count > 0 Then
RollbackTransaction()
Throw ExceptionGroup
End If
End Sub
我知道这可能看起来很复杂,但是您应该能够推断出第一块代码捕获了第二块代码中抛出的 ExceptionGroup。
这基本上是系统的一部分,其中对象是从数据库中的行创建的,当第一次需要其中一个对象时(即,为每一行创建许多特定类型的对象,然后提取数据用于每个仅在第一次请求时。每个对象都存储一个 DataRow 实例,对象内的属性从该实例中提取第一次请求属性值时所需的数据。 p>
我的观点是,这是一个我从头开始制作的系统,没有第三方代码,也没有使用 Linq 或任何其他类似的花哨的东西(在有人说要切换到 Linq 或类似的东西之前,我也不想这样做)。
正如您所知,ExceptionGroup 由每个属性的验证引发的其他异常填充。
现在问题来了。在没有 try catch 语句运行第一块代码时,它会抛出一个可怕的红色和黄色错误屏幕。但是使用 try catch 语句,它可以完美加载。
有谁知道是什么导致了这种奇怪的行为?有没有人遇到过这种行为?
提前致谢。
问候,
理查德
【问题讨论】:
-
顺便说一句,我尝试遍历 ExceptionGroup 中包含的所有异常,并从中输出消息/堆栈跟踪。我也尝试在母版页的面板中添加控件,但无济于事。
-
组中有哪些例外?
-
这就是我无法找到的。根据验证函数中的代码(现在,它已经 5 个月大了,从那以后我就没有碰过它),它应该是 Exceptions.ItemExceptions.RequiredFieldException 或 Exceptions.ItemExceptions.InvalidFormatException。它也可能是 System.Exception。但是,如果捕获到 ExceptionGroup(如果 Message 和 StackTrace 为空),我尝试简单地输出“Hello World”,但它仍然什么也没说。然而删除 try catch 并抛出 ExceptionGroup 类型的异常但未捕获......
-
我刚刚尝试捕获 System.Exception,然后通过 Response.Write 输出“Hello World”。我搜索了“Hello”的源代码,但一无所获……我只是不知道是什么原因造成的。过去一天我做了太多更改,无法全部撤消。
-
我刚刚想到一件事——try catch 语句在 page_preinit 方法中。我不确定您是否可以在preinit中输出到页面,可以吗?如果不是,那么这可能就是问题所在。我尝试在 try catch 中抛出异常,发现它成功抛出了新异常。但是,我原以为尝试在 preinit 方法中输出会引发异常。无论成功与否,我都会尝试一些东西并回到这里
标签: .net asp.net vb.net exception exception-handling