This is a discussion on Python’s forums about adding something akin to a throws keyword in python.

    • Nevoic@lemm.ee
      link
      fedilink
      arrow-up
      9
      ·
      1 year ago

      Yes, but not because the goal of having exceptions in types is bad, rather Java’s type system isn’t advanced enough to support the ideal solution here.

      Scala 3 is working on experimental capture checking capabilities, which allows functions to express certain capabilities (file access, networking, db, etc.), and CanThrow capabilities (e.g exceptions at the type level) are one reification of this.

      The CanThrow docs I linked have a good introduction into why Java checked exceptions are bad, and how Scala’s alternative is far better. Essentially it comes down to a lack of polymorphism in checked exceptions. In practice this means they’re incredibly verbose outside of simple usecases, and with a very easy escape hatch (RuntimeException), you don’t even get the guarantee of knowing a function without checked exceptions doesn’t throw.

      Python will also have this latter issue. Python’s “typing” in general has this issue actually. Types aren’t validated unless you use an external tool, and even then Any is a leaky abstraction that can hide any level of typing errors, unlike in properly typed languages where it’s not leaky. You need it to be leaky in gradually typed environments, or you wouldn’t be able to use a ton of the Python ecosystem, but this vastly reduces the effectiveness of the typing solution.

      I don’t know if Python’s solution here will address the lack of polymorphism that Java’s solution has, I’ll have to look into it more.

  • onlinepersonaOP
    link
    fedilink
    English
    arrow-up
    9
    arrow-down
    1
    ·
    1 year ago

    When I used to write Java and switched to Python, this was one of the things I missed. It was always quite clear which exceptions I had to catch (or not). Just today, I ran into the issue of trying to cover the exceptions a library could throw without using except: or except Exception as e, but finally gave up and gave in to it. The linter wasn’t happy, but fuck it.

    • Fal@yiffit.net
      link
      fedilink
      English
      arrow-up
      1
      arrow-down
      9
      ·
      1 year ago

      It was always quite clear which exceptions I had to catch (or not)

      Lol. You’re literally the only one that likes checked exceptions. And, it seems you think that it actually gives you information about what exceptions to catch. It does not. Most things just catch and rethrow as a RuntimException

      • qwertyasdef
        link
        fedilink
        arrow-up
        9
        ·
        1 year ago

        Hey, I like checked exceptions too! I honestly think it’s one of Javas’s best features but it’s hindered by the fact that try-catch is so verbose, libraries aren’t always sensible about what exceptions they throw, and methods aren’t exception-polymorphic for stuff like the Stream API. Which is to say, checked exceptions are a pain but that’s the fault of the rest of the language around them and not the checked exceptions per se.

      • CodeMonkey
        link
        fedilink
        arrow-up
        7
        ·
        1 year ago

        I also like checked exceptions. I like having a compile time check that I thought through error scenarios.

        Is it perfect? No, but it should be iterated upon, not discarded.

        FYI, catching and rethrowing as an unchecked exception is a pretty bad anti-patern (and a foul code smell).

  • sugar_in_your_tea@sh.itjust.works
    link
    fedilink
    arrow-up
    4
    arrow-down
    1
    ·
    edit-2
    1 year ago

    I disagree, I’d instead like to move toward handling errors as logic, and keeping exceptions for actually exceptional cases. If you’re expecting an exception, that’s data.

    So here’s my proposal:

    • introduce monads like Maybe/Result that forces the dev to handle expected errors in logic
    • make an easy way to return errors early without interrupting logic flow
    • simplify checking for None values in chaining

    For the first (not exactly a monad, may need a new type to wrap things):

    def maybe_err(val: int) -> Result[int, ValueError]:
        if val < 0:
            return ValueError("cannot be negative")
        return val
    
    match (val := maybe_err(-1)):
        case int():
        case ValueError():
    

    For the second:

    val = maybe_error(-1)?  # special handling to return instances of Error early
    

    And the third:

    val = x?.y?.z ?? DEFAULT
    

    I like this much better than having try/except blocks throughout the code, and reserve those only for logging and whatnot at the top level. If you document exceptions, people will use them even more as data instead of exceptions.

    So only raise if you want it to bubble all the way up, return errors if it’s just data for the caller. Libraries should almost never raise.

      • sugar_in_your_tea@sh.itjust.works
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        1 year ago

        I’m not too familiar with C# (last used it like a decade ago), but I think the rules here would be pretty simple:

        • x? - if x is None or an Error, return from the function early, otherwise use the value and continue
        • x?.y - same as above, but with an attribute of x
        • x ?? y - instead of returning as in the first, use y as the default value

        And maybe add an option to convert exceptions from a function to an Error value (maybe some_func?() to convert to error values? IDK, I haven’t thought through that part as much).

        Hopefully that’s simple enough to be useful.

        If I were proposing this, I’d limit it to optional chaining since that’s far more annoying to me currently.

      • sugar_in_your_tea@sh.itjust.works
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

        My point is that I don’t like using exceptions for communicating regular errors, only unrecoverable faults. So adding features to document exceptions better just doesn’t feel like the right direction.

        Maybe that’s un-Pythonic of me, idk. From the zen of Python:

        Errors should never pass silently.
        Unless explicitly silenced.

        Using monads could let programmers silently pass errors.

        I just really don’t like the exception model after years of using other languages (mostly Rust and Go), I much prefer to be forced to contend with errors as they happen instead of just bubbling them up by default.

        • twoframesperminute@mastodon.social
          link
          fedilink
          arrow-up
          0
          ·
          1 year ago

          @sugar_in_your_tea The idea of exceptions is that you can choose when to deal with them. So if you want to deal with them immediately,
          nothing is stopping you.

          If you think handling errors with every function call explicitly is easier, I guess you’re using very few functions. For the project I’m working on, your proposal would probably double the number of lines. Thanks, but no thanks.

          • sugar_in_your_tea@sh.itjust.works
            link
            fedilink
            arrow-up
            1
            ·
            edit-2
            1 year ago

            Handling can mean a lot of things. You can use a sigil to quickly return early from the function without cluttering up your code. For example, in Rust (code somewhat invalid because I couldn’t post the generic arg to Result because lemmy formatting rules):

            fn my_func() -> Result {
                let val = some_func_that_can_error()?;
                return Some(val.operation_that_can_error());
            }
            
            let val = match my_func() {
                Err(err) => {
                    println!("Your error: {err}");
                    return;
                }
                Some(val) => val,
            };
            // use val here
            

            That question mark inside my_func shows the programmer that there’s a potential error, but that the caller will handle it.

            I’m suggesting something similar for Python, where you can easily show that there’s a potential error in the code, without having to do much to deal with it when it happens if the only thing you want to do is bubble it up.

            If we use exceptions, it isn’t obvious where the errors could occur, and it’s easy to defer handling it much too late unless you want to clutter your code.

            • twoframesperminute@mastodon.social
              link
              fedilink
              arrow-up
              0
              ·
              1 year ago

              @sugar_in_your_tea I’m by far not qualified to discuss this in depth. But it seems to me that almost every function call ever can fail. Therefore, do you need to do this with every single function call?

              That seems terribly inefficient and bloated. How is that readable for anyone?

              • sugar_in_your_tea@sh.itjust.works
                link
                fedilink
                arrow-up
                1
                ·
                1 year ago

                That’s where the difference between exceptional cases comes in. Rust and Go both have the concept of a panic, which is an error that can only be caught with a special mechanism (not a try/except).

                So that’ll cover unexpected errors like divide by zero, out of memory, etc, and you’d handle other errors as data (e.g. record not found, validation error, etc).

                I don’t think Python should necessarily go as far as Go or Rust, just that handling errors like data should be an option instead of being forced to use try/except, which I find to be gross. In general, I want to use try/except if I want a stack trace, and error values when I don’t.

                • twoframesperminute@mastodon.social
                  link
                  fedilink
                  arrow-up
                  0
                  ·
                  edit-2
                  1 year ago

                  @sugar_in_your_tea But isn’t all that possible in Python? Don’t monads cover exactly what you want? Why does it need to be implemented some different way?

                  Also, divide by zero should be data just as well. Failing to program around having nothing to divide by is not a reason to have a program panic.

                  Also, having two systems for largely the same behavior doesn’t seem to improve usability and clarity, in my opinion.