情况如下。 Crystal Reports在.net-4.6中存在一些问题,并在尝试加载System.IO.FileNotFoundException
时抛出异常(crdb_adoplus.dll
,调用堆栈从CrystalDecisions.ReportAppServer.DataSetConversion.DataSetConverter.DataSetProcessingDelegate
开始 - 这意味着,我不能将它包装在try-catch-block中。要至少记录此类问题,请将AppDomain.CurrentDomain.UnhandledException
- 处理程序设置为Sub Main
中的第一个操作。并且它正常工作,但仅如果我从Visual Studio运行应用程序,如果应用程序直接运行,则无法正常工作。
P.S。我知道如何使Crystal Reports工作(useLegacyV2RuntimeActivationPolicy
),我只是在寻找处理此类错误的方法。
Update1 :它是Windows窗体应用程序,而AppDomain.CurrentDomain.UnhandledException
和Application.ThreadException
的两个处理程序正在运行(后者与Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, True)
),但由于某种原因,这个特殊的异常不会(但只有在不从Visual Studio中启动时才会...)
Sub Main
如下所示。
<STAThread()>
Sub Main()
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledThreadExceptionHandler
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, True)
'many lines of code...
AddHandler Application.ThreadException, AddressOf ThreadExceptionHandler
Application.Run(x)
End Sub
Update1.1 :(忘了处理程序)
Private wsExNr As Integer = 0
Friend Sub UnhandledThreadExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
Dim ex = TryCast(e.ExceptionObject, Exception)
If ex IsNot Nothing Then
ex.Data.Add("isUnhandledThreadExceptionHandler", True)
ex.Data.Add("isMainThread", (Threading.Thread.CurrentThread.ManagedThreadId = wsMainThreadId))
If sender IsNot Nothing Then
ex.Data.Add("Sender", sender.GetType.FullName)
End If
End If
Dim path = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Exception_Log")
IO.Directory.CreateDirectory(path) 'no need in checking if it exists etc -- it's built-in.
Dim nr = Threading.Interlocked.Increment(wsExNr)
Dim timestamp_str = ""
Try
timestamp_str = Now.ToString("yyyyMMddhhmmss.fff")
Catch : End Try
Using wr As New IO.StreamWriter(IO.Path.Combine(path, "ex" & timestamp_str & "_" & nr & ".txt"))
wr.WriteLine("UnhandledThreadExceptionHandler!")
If ex IsNot Nothing Then
wr.WriteLine(ex.ToString)
End If
End Using
If ex IsNot Nothing Then
If Core.Visual.Dialogs.Internal.MainWindow IsNot Nothing AndAlso Core.Visual.Dialogs.Internal.MainWindow.IsHandleCreated Then
Core.Visual.Dialogs.Internal.MainWindow.Invoke(New Action(Of Exception)(AddressOf ShowUnhandledThreadException), ex)
Else
MsgBox("UnhandledException:" & vbNewLine & ex.ToString)
End If
Else
MsgBox("UnhandledException:" & vbNewLine & e.ExceptionObject.ToString)
End If
End Sub
Private Sub ShowUnhandledThreadException(ByVal ex As Exception)
'this is basically a MsgBox
Core.Visual.Dialogs.ExceptionDialog.Show(
ex,
Diagnostics.EventLogEntryType.Error,
True)
End Sub