[solved] How to prevent repeating exceptions in console for async void method?

I’ve posted the same question in Unity Answers 9 days ago and got no answer. I hope this isn’t against some guideline to seek help here as well.

If I attach the following MonoBehaviour to an active GameObject (e.g. Camera) and then start the application (play in Editor), I get an indefinitely repeating exception logging in the console of the Editor. I would expect that it would log the exception once and not continuously repeating it.

using System;
using System.Threading.Tasks;
using UnityEngine;

public class TestController
     : MonoBehaviour
{
     [SerializeField]
     private int m_counter;
    
     public async void TestIt()
     {
         if(m_counter > 0)
         {
             return;
         }
        
         await Task.Delay(1);
         ++m_counter;
         throw new Exception("Once");
     }
    
     private void Update()
     {
         TestIt();
     }
}

The field m_counter stays at 1. So no new exception is thrown. It doesn’t even stop when I stop playing the app. It will continue to spam the console in the Editor. I have to restart the Editor to stop it.

Can somebody explain to me why this is the case and how I can prevent it? I’m using the Unity Editor 2018.3.3f1 on Linux.

1 Like

Anyone finding this via a search. Look at the question on Unity Answer.

Issue is tracked here: Unity Issue Tracker - Exception thrown in one async tasks stops all other async methods
Also isn’t happening in version 2018.3.2.

1 Like