this post was submitted on 15 Nov 2023
24 points (87.5% liked)

Python

6496 readers
1 users here now

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

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[–] sugar_in_your_tea 1 points 1 year ago* (last edited 1 year ago) (1 children)

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.