0

What could be the reason why an exception thrown by the NHibernate LINQ provider wouldn't be caught by a catch block? As far as I know, there is no threading involved. It's a .NET 4.5.2 x64 Windows Service app running on Windows Server 2008 R2 + SQL Server 2008 R2.

Code is basically doing some simple URL shortening, so this method is just checking if a mapping for the URL already exists.

Calling code is something like:

// 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);
}

I don't even see what could possibly fail in the LINQ Provider, since the query is quite simple, but anyway the app crashed and the exception wasn't caught by the handler (I also didn't have the AppDomain.UnhandledException handler attached in this app), so the only information I got afterwards was from the Event Log:

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()

(Update)

Thanks to comment by @starlight54 pointing to this thread (System.Reflection.TargetInvocationException not being caught), I noticed that there was an additional entry in the event log, from the Windows Error Reporting Service, which said that in fact an AccessViolationException happened:

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: 

(Update 2)

It seems that the first exception logged is a "red herring", i.e. isn't the actual point where the exception happens, and since .NET 4 AccessViolationExceptions are not caught unless the exception handler method is decorated with both [SecurityCritical] and [HandleProcessCorruptedStateExceptions] attributes. So finally I added these according to the answers in this thread and now I am waiting for the exception to happen.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Lou
  • 4,244
  • 3
  • 33
  • 72
  • Are you sure the DLL is up to date and contains the catch block? Could the error be occurring somewhere else? – user1666620 Oct 10 '16 at 09:20
  • 1
    Possibly due to this? http://stackoverflow.com/questions/29682162/system-reflection-targetinvocationexception-not-being-caught – starlight54 Oct 10 '16 at 09:24
  • @starlight54: Thanks a lot, seems like it might be. I commented on the answer so I hope the OP from that thread will provide some additional info. – Lou Oct 10 '16 at 09:41

0 Answers0