1
votes

I have a simple async method that loads an entity asynchronously using LoadAsync. I can't catch exceptions thrown by the async call. Debugging just leads to 'AggregateException was unhandled by user code.' I'm using RavenDB Client 2.0.2375.

How can I catch these exceptions?

Here's the method:

 private async Task<Dummy> GetDummyAsync(string id)
    {
        using (var session = docStore.OpenAsyncSession())
        {
            try
            {
                var dummy = await session.LoadAsync<Dummy>(id);
                return dummy;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }

Edit

Here's the exception:

System.AggregateException was unhandled by user code HResult=-2146233088 Message=One or more errors occurred. Source=mscorlib StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at Raven.Client.Connection.HttpJsonRequest.<>c_DisplayClassc.b_9() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:line 128 at Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func1 getResponse) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:line 351 at Raven.Client.Connection.HttpJsonRequest.<InternalReadResponseStringAsync>b__8(Task1 task) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:line 128 at System.Threading.Tasks.ContinuationResultTaskFromResultTask2.InnerInvoke() at System.Threading.Tasks.Task.Execute() InnerException: System.Net.WebException HResult=-2146233079 Message=The remote server returned an error: (404) Not Found. Source=System StackTrace: at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task`1 promise, Boolean requiresSynchronization) InnerException:

1
This will probably help you find the solution: interact-sw.co.uk/iangblog/2010/11/01/csharp5-async-exceptions - L01NL
Do you see same behavior without attaching debugger ? - YK1
Perhaps exception is thrown by OpenAsyncSession? What is stack trace? What is in the InnerExceptions ? - YK1
The exceptions change depending on what I'm doing. If I request an invalid document they're HTTP 404 exceptions. I can catch the exceptions with a UnhandledExceptionTrapper but that's really not what I want to do. Maybe I'm misunderstanding something but at 4 in the morning I'm starting to think there's a bug in raven. I'll dig into this more tomorrow. - Mark
The inner exception doesn't matter. For testing I just load a document that doesn't exist and the inner exception is a System.Net.WebException: " Message=The remote server returned an error: (404) Not Found." - Mark

1 Answers

4
votes

The behavior your seeing is valid, and is not because you couldn't catch the exception. It's because the exception was already caught for you.

In RavenDB, if you try to load a document that doesn't exist, the Load or LoadAsync methods will return null. They will not throw an exception.

When you are debugging, you are seeing that under the hood a WebException is thrown when the HTTP response comes back as 404. That exception is handled. You should see it in the output window as a "First Chance Exception".

This is all normal behavior. If you want to check that you can catch exceptions, try something that will actually throw an exception. Perhaps load a document of one type while trying to cast it into another. That will certainly throw an exception.