﻿# V3231\. Async method returning 'void' throws an exception\. Callers will not be able to catch this exception\. Consider replacing the return type with 'Task'\.

The analyzer detected a method with the async modifier and the void return type\. An exception is thrown inside this method\. Exceptions within such methods cannot be caught or handled by the calling code because the method does not return the Task object, which is used to await the completion of the asynchronous operation and to handle exceptions\.

According to [guidelines](https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/async-return-types#void-return-type), these methods should not be used outside event handlers\. This is because they cannot be used with `await`, their completion cannot be controlled and exceptions thrown within them cannot be handled in the calling context\. As a result, unhandled exceptions may lead to process crash\.

The example: 

```cpp
async void LoadDataAsync(string data)
{
  await Task.Delay(100);

  if (data == null)
    throw new InvalidOperationException("Data loading error.");

  ....
}

void Start(string data)
{
  try
  {
    LoadDataAsync(data);
  }
  catch
  {
    ....
  }
}
```

The exception thrown in the `LoadDataAsync` method will not be caught in the `Start` method, as `LoadDataAsync` is the `async` method with the `void` return type\.

The fixed example:

```cpp
async Task LoadDataAsync(string data)
{
  await Task.Delay(100);

  if (data == null)
    throw new InvalidOperationException("Data loading error.");
    
  ....
}

async Task Start(string data)
{
  try
  {
    await LoadDataAsync(data);
  }
  catch
  {
    ....
  }
}
```

Now the asynchronous operation is represented by the `Task` object\. This allows the calling context to await its completion and handle the exception\.