Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

IL2CPP Exception filter support ETA?

Discussion in 'Experimental Scripting Previews' started by Prodigga, Apr 5, 2021.

  1. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Hello, is there an ETA for exception filter support for IL2CPP. I've started writing a fair bit of async/await code and am running into some places where exception filtering would be nice to have.

    There is also nothing suggesting that the feature is broken... I thought it was supported and working this whole time! (it compiles and runs fine in the editor), but then I just happened upon this in the documenation:
    https://docs.unity3d.com/Manual/ScriptingRestrictions.html

    And, uhh, yeah, this was news to me as we are using some exception filtering in one of our production apps. There is no hint or anything that this feature doesn't work.

    Anyway, just wondering if this will be supported at some point, and if not is it possible to detect it and have the editor tell us that it wont work?
     
  2. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    It seems like Jackson Dunstan has encountered some issues with exception filtering here in his post, too:
    https://www.jacksondunstan.com/articles/4702

    It seems like the feature was attempted to be supported but not quite there yet? I can't find the bug report. Wondering if there was an update on it?
     
  3. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    We don't have an ETA for full exception filter support. At the moment many exception filters will work, but there are some complex exception filter cases which don't work.

    The bad news around exception filters is that we don't have good documentation or diagnostics to allow you to know the difference (as you have likely discovered).

    So far, the need for exception filters has been low enough that adding complete support has not been a priority.

    Are you willing or able to share the use case you have? I'd love to better understand how they work and don't work in your cases.
     
  4. SurprisedPikachu

    SurprisedPikachu

    Joined:
    Mar 12, 2020
    Posts:
    84
    A common use case of Exception filters is when you want to handle Task cancellation:

    Code (CSharp):
    1. public async Task<int> BarAsync()
    2. {
    3.     try
    4.     {
    5.         var x = await FooAsync();
    6.         return x * 2;
    7.     }
    8.     catch (Exception ex) when (!(ex is OperationCanceledException)) // when (ex is not OperationCanceledException) at C# 9.0
    9.     {
    10.         return -1;
    11.     }
    12. }
    Is it supported?
     
  5. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Yes, this use of except filters should work. Although, couldn't you accomplish the same behavior without exception filters?

    Code (CSharp):
    1. public async Task<int> BarAsync()
    2. {
    3.     try
    4.     {
    5.         var x = await FooAsync();
    6.         return x * 2;
    7.     }
    8.     catch (Exception ex)
    9.     {
    10.         if (!(ex is OperationCanceledException))
    11.             return -1;
    12.         throw;
    13.     }
    14. }
     
    SurprisedPikachu likes this.
  6. SurprisedPikachu

    SurprisedPikachu

    Joined:
    Mar 12, 2020
    Posts:
    84
    Yes, your example works too. I just wanted to make sure if I need to look for exception filters used by plugins or not.
     
  7. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Hey sorry I forgot to get back around to this. Is there a list of scenarios where it wont work? What determines whether it would work or not? Why would the example above work?
     
    bdovaz likes this.
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Unfortunately no, we don't have a clear list of what will and won't work. This is the biggest part of the problem, as it makes the use of exception filters difficult. That is why we don't recommend their use.

    Some very simple cases, with just a single catch statement with an exception filter will work. More complex ones that involved multiple catch statements usually don't work.

    The example I provided above does not use exception filters. :)