抛出的异常没有被`catch`块捕获

时间:2016-10-10 08:51:50

标签: c# .net sql-server nhibernate try-catch

NHibernate LINQ提供程序抛出的异常不会被catch捕获的原因是什么?据我所知,没有涉及线程。它是在Windows Server 2008 R2 + SQL Server 2008 R2上运行的.NET 4.5.2 x64 Windows服务应用程序。

代码基本上是做一些简单的URL缩短,所以这个方法只是检查URL的映射是否已经存在。

调用代码就像:

// triggered by a System.Timers.Timer event
try
{
    // the purpose of this hash is to let SQL Server 
    // use a simple index, instead of a full-text search

    var hash = UrlMapping.CalculateHash(url);
    var id = Session  // <-- NHibernate.ISession
        .Query<UrlMapping>()
        .Where(map => map.UrlHash == hash && map.Url == url)
        .Select(map => (long?)map.Id)
        .FirstOrDefault();

    return id;
}
catch (Exception ex)
{
    Log.Error(ex);
}

我甚至看不到LINQ提供程序中可能出现的问题,因为查询非常简单,但无论如何应用程序崩溃并且异常没有被处理程序捕获(我也没有&#39 ; t在此应用程序中附加了AppDomain.UnhandledException处理程序),因此我之后获得的唯一信息来自事件日志:

Application: MyApp.ServiceRunner.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Reflection.TargetInvocationException
Stack:
   at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])
   at System.Delegate.DynamicInvokeImpl(System.Object[])
   at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NHibernate.Linq.NhLinqExpression, NHibernate.IQuery, NHibernate.Linq.NhLinqExpression)
   at NHibernate.Linq.DefaultQueryProvider.Execute(System.Linq.Expressions.Expression)
   at System.Linq.Queryable.FirstOrDefault(System.Linq.IQueryable`1<System.Nullable`1<Int64>>)
   at MyApp.Data.Repositories.UrlMappingRepo.GetKeyForUrl(System.String)
   at MyApp.Data.Repositories.UrlMappingRepo.GetOrCreateKeyForUrl(System.String, System.String)
   at MyApp.Timers.Timer.OnElapsed(System.DateTime)
   at MyApp.Timers.Timer.FireWithExceptionHandling(System.DateTime)
   // there is a try/catch here ^^^
   at MyApp.Timers.Timer.InternalTimerSkip_Elapsed(System.Object, System.Timers.ElapsedEventArgs)
   at System.Timers.Timer.MyTimerCallback(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.TimerQueueTimer.CallCallback()
   at System.Threading.TimerQueueTimer.Fire()
   at System.Threading.TimerQueue.FireNextTimers()

(适用更新)

感谢@ starlight54指向this thread的评论(System.Reflection.TargetInvocationException未被捕获),我注意到事件日志中有一个额外的条目,来自Windows错误报告服务,它说实际上发生了AccessViolationException

Fault bucket , type 0
Event Name: CLR20r3
Response: Not available
Cab Id: 0

Problem signature:
P1: MyApp.ServiceRunner.exe
P2: 1.1.0.1436
P3: 57ed0aa0
P4: System.Core
P5: 4.0.30319.34209
P6: 53489a70
P7: 39c
P8: 54
P9: System.AccessViolationException
P10: 

(更新2)

似乎记录的第一个异常是&#34; red herring&#34;,即不是异常发生的实际点,并且因为没有捕获.NET 4 AccessViolationException除非异常处理程序方法使用[SecurityCritical][HandleProcessCorruptedStateExceptions]属性进行修饰。所以最后我根据answers in this thread添加了这些,现在我正在等待异常发生。

0 个答案:

没有答案