• TimeSquirrel@kbin.melroy.org
    link
    fedilink
    arrow-up
    9
    ·
    5 days ago

    I’m just a hobbyist but…are you guys using exceptions like they’re conditional statements?? I thought those were for only when shit is seriously wrong and execution can’t continue in the current state. Like if some resource was in a bad state or some input was malformed.

    Or maybe I haven’t worked on anything complex enough, I dunno.

    • sbv@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      13
      ·
      5 days ago

      I thought those were for only when shit is seriously wrong and execution can’t continue in the current state.

      That’s how it starts. Nice and simple. Everyone understands.

      Until

      some resource was in a bad state

      and you decide you want to recover from that situation, but you don’t want to refactor all your code.

      Suddenly, catching exceptions and rerunning seems like a good idea. With that normalized, you wonder what else you can recover from.

      Then you head down the rabbit hole of recovering from different things at different times with different types of exception.

      Then it turns into confusing flow control.

      The whole Result<ReturnValue,Error> thing from Rust is a nice alternative.

    • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
      link
      fedilink
      arrow-up
      11
      ·
      5 days ago

      As a rule, exceptions should indeed be used for behaviors that are outside normal execution flow. For example, you might throw an exception if a file you’re trying to access doesn’t exist, or a network call fails, etc.

    • tunetardis@lemmy.ca
      link
      fedilink
      English
      arrow-up
      4
      ·
      5 days ago

      I suppose it depends on the language? For the most part I think you’re right. Exceptions are only used (if at all) in situations where a program diverges unexpectedly from its normal flow. But take a language like Python. They’re just everywhere. Even your plain old for loop ends on an exception, and that’s just business as usual.

    • Ephera@lemmy.ml
      link
      fedilink
      arrow-up
      3
      ·
      5 days ago

      You don’t want to use exceptions in normal control flow, because they’re extremely slow. Every time you throw an exception, it has to collect a stacktrace, which is hundreds, if not thousands, of calculations, compared to a handful of calculations for returning a boolean or an enum variant.