Search Unity

Question Preventing Specific Exceptions from Logging in Unity Console

Discussion in 'Scripting' started by legendforever, Jul 28, 2023.

  1. legendforever

    legendforever

    Joined:
    Nov 11, 2017
    Posts:
    2
    Hi all,
    I'm working on a script that has an async initialization method. Inside this method, there is a loop that waits for a certain condition before continuing. If the game object is deactivated or the editor stops playing, I want to gracefully exit the loop by throwing an OperationCanceledException.

    The problem is that I don't want this particular exception to show up in the console, since it's not really an error condition.

    CEx.JPG

    Code (CSharp):
    1.  
    2. public async void Init()
    3. {
    4.   // ...
    5.   while (true)
    6.   {
    7.     if (Path.Find(_pos))
    8.     {
    9.        // ...
    10.        break;
    11.     }
    12.    
    13.     if (!Application.isPlaying || !gameObject.activeInHierarchy)
    14.     {
    15.       throw new OperationCanceledException(); //=====> Don't want to log this exception
    16.     }
    17.    
    18.     await Task.Yield();
    19.   }
    20.   // ...
    21. }
    22.  
    Is there any way I can prevent certain custom exceptions like this from logging? I've tried looking into LogFilter but haven't quite figured out how to exclude specific exception types.

    I don't want to use a Coroutine in this particular situation since I need the async/await pattern.

    Any tips would be appreciated!
    Thanks!