For context, I am trying to do a save system for a game.

  • milis@programming.dev
    link
    fedilink
    arrow-up
    4
    arrow-down
    1
    ·
    2 months ago

    I saw long time ago from somewhere saying that handling exception is expensive in terms of stack operations. To avoid the unexpected I guess you should do both, but a check before loading just saves you from unnecessary exception handlings which, if the very first statement is indeed true, would harm the performance.

    • GetOffMyLan@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      2 months ago

      It is an expensive operation as it needs to unwind the stack to get the stack trace.

      But if you’re checking a single file you won’t notice it.

      If you’re doing it in a tight loop it’ll be very noticeable.

      Checking the file exists also has a cost.

      I likely wouldn’t bother if I was saving a single file especially as there are other exceptions that could be thrown in the process.