Search Unity

Resolved Visual Scripting - How to catch system exceptions

Discussion in 'Authoring Dev Blitz Day 2023' started by Marou1, Jan 26, 2023.

  1. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    Hello,

    I want to catch system exceptions to do something like this:

    Code (CSharp):
    1. try
    2. {
    3. SaveData(data);
    4. }
    5. catch(System.IO.IOException)
    6. {
    7. DisplayMessage("The file is open elsewhere or there is not enough storage space");
    8. }
    9. catch(System.Security.SecurityException)
    10. {
    11. DisplayMessage("Could not save as you do not have the required permissions");
    12. }
    ...

    Basically I need to catch these 4 exceptions:
    System.IO.IOException
    System.Security.SecurityException
    System.FormatException
    System.ArgumentException

    I added System Exception in the nodes in Project Settings, but I cannot find these exceptions. Also, I am not sure how to use the Try&Catch node.

    Thanks
     
  2. jeanedouard_unity

    jeanedouard_unity

    Unity Technologies

    Joined:
    May 2, 2019
    Posts:
    49
    To add an Exception type just go to the Visual Scripting Project Settings and add it to the Type options list. After clicking on the Regenerate Nodes button you should be able to see the nodes related to the exception type you just added.
    Now for the try/catch node, unfortunately there seems to be an issue as after trying on version 1.8.0, I can't get the exception types displayed in the dropdown menu of the node. I'll log that issue and make sure to prioritize this for the next VS update.
     
    Marou1 likes this.