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

You are viewing a single thread.
View all comments View context
3 points

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.

permalink
report
parent
reply
0 points

@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.

permalink
report
parent
reply
1 point
*

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.

permalink
report
parent
reply
0 points

@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?

permalink
report
parent
reply

Python

!python@programming.dev

Create post

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events
October 2023
November 2023
Past

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
  • Pythörhead: a Python library for interacting with Lemmy
  • Plemmy: a Python package for accessing the Lemmy API
  • pylemmy pylemmy enables simple access to Lemmy’s API with Python
  • mastodon.py, a Python wrapper for the Mastodon API
Feeds

Community stats

  • 470

    Monthly active users

  • 438

    Posts

  • 2.1K

    Comments