Search Unity

how to catch InvalidKeyException ?

Discussion in 'Addressables' started by Arcanor, Apr 10, 2020.

  1. Arcanor

    Arcanor

    Joined:
    Nov 4, 2009
    Posts:
    283
    I'm just getting started with Addressables. My first goal is to handle the exception when the resource path is incorrect. I'm setting up what I think should be a very simple test case, where the path doesn't exist:
    Code (CSharp):
    1.     private void TextureHandle_Completed(AsyncOperationHandle<Texture2D> handle)
    2.     {
    3.         if (handle.Status == AsyncOperationStatus.Succeeded)
    4.         {
    5.             Texture2D result = handle.Result;
    6.             // The texture is ready for use.
    7.             Debug.Log("Got it");
    8.         }
    9.         else
    10.         {
    11.             Debug.LogWarning("Didn't get it");
    12.         }
    13.     }
    14.  
    15.     void Start()
    16.     {
    17.         AsyncOperationHandle<Texture2D> textureHandle = new AsyncOperationHandle<Texture2D>();
    18.         try
    19.         {
    20.             textureHandle = Addressables.LoadAssetAsync<Texture2D>("path_that_doesnot_exist");
    21.         }
    22.         catch (System.Exception e)
    23.         {
    24.             Debug.LogWarning("EXCEPTION: " + e);
    25.             return;
    26.         }
    27.         textureHandle.Completed += TextureHandle_Completed;
    28.     }
    However, there's an unhandled exception being thrown from down in the addressables package:
    Code (CSharp):
    1. Exception encountered in operation UnityEngine.ResourceManagement.ResourceManager+CompletedOperation`1[UnityEngine.Texture2D], result='', status='Failed': Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown., Key=path_that_doesnot_exist, Type=UnityEngine.Texture2D
    2. UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationBase`1:<.ctor>b__33_0(AsyncOperationHandle)
    3. DelegateList`1:Invoke(AsyncOperationHandle) (at Library/PackageCache/com.unity.addressables@1.7.5/Runtime/ResourceManager/Util/DelegateList.cs:69)
    4. UnityEngine.AddressableAssets.Initialization.<>c__DisplayClass14_0:<LoadContentCatalogInternal>b__0(AsyncOperationHandle`1)
    5. DelegateList`1:Invoke(AsyncOperationHandle`1) (at Library/PackageCache/com.unity.addressables@1.7.5/Runtime/ResourceManager/Util/DelegateList.cs:69)
    6. UnityEngine.ResourceManagement.ChainOperation`2:OnWrappedCompleted(AsyncOperationHandle`1)
    7. DelegateList`1:Invoke(AsyncOperationHandle`1) (at Library/PackageCache/com.unity.addressables@1.7.5/Runtime/ResourceManager/Util/DelegateList.cs:69)
    8. UnityEngine.ResourceManagement.ResourceManager:Update(Single)
    9. MonoBehaviourCallbackHooks:Update() (at Library/PackageCache/com.unity.addressables@1.7.5/Runtime/ResourceManager/Util/MonoBehaviourCallbackHooks.cs:19)
    So apparently my attempt to try-catch isn't working, presumably because the call is async.

    How can I catch exceptions thrown by async operations?
     
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    You can access the exception from the handle in the Completed callback. You will need to type check it for the exception type you're looking for.
    if (handle.OperationException is UnityEngine.AddressableAssets.InvalidKeyException invalidKeyException) { // handle invalid key exception }


    [EDIT] If you're getting extra error logs that you're not expecting, you can turn those off in the top level addressable settings.
     
  3. Arcanor

    Arcanor

    Joined:
    Nov 4, 2009
    Posts:
    283
    Aha, thank you! I had already figured out how to deal with the exception after the fact, using handle.OperationException, but the runtime exception was constantly stopping my editor play test. I didn't know about the setting in AddressableAssetSettings, so that was the clue that solved my issue.
    Now that I've unchecked "Log Runtime Exceptions", my testing can continue without pausing. Thanks again!