Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

agnos.is Forums

  1. Home
  2. Programmer Humor
  3. Slapping on a `.expect` is also error handling!

Slapping on a `.expect` is also error handling!

Scheduled Pinned Locked Moved Programmer Humor
programmerhumor
25 Posts 18 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • dgriffith@aussie.zoneD [email protected]

    causing the program to crash if it actually was an error, restoring the more unsafe behavior of other languages.

    Wellllll it's more of an abrupt exit rather than a crash, which is still better than eg. silently accessing beyond the end of an array, or ending up with a pointer to nowhere when you thought you had a sane memory reference.

    A This user is from outside of this forum
    A This user is from outside of this forum
    [email protected]
    wrote last edited by
    #14

    “An abrupt exit”, more commonly known as a “crash”.

    If you’re going to argue that an exit through panic!() is not a crash, I will argue that your definition of a crash is just an abrupt exit initiated by the OS. In other words, there’s no meaningful distinction as the result is the same.

    Q dgriffith@aussie.zoneD 2 Replies Last reply
    2
    • ediacarium@feddit.orgE [email protected]

      Languages like Java or C++ have Exceptions, which are errors, that are not explicitly mentioned in the function signature. Meaning, you might need to handle an exception you didn't even know existed. And if you don't, your program will just crash when these exceptions occur.

      In Rust all errors are explicitly mentioned and part of the return type.
      Because of this Rust has a lot of ways to quickly handle an error. One of those ways is "Trust me, bro" (or .unwrap()), which converts the combined error/success return type into just a success type, causing the program to crash if it actually was an error, restoring the more unsafe behavior of other languages.

      T This user is from outside of this forum
      T This user is from outside of this forum
      [email protected]
      wrote last edited by
      #15

      Crashing through unwrap is not necessarily restoring unsafe behavior of other languages though. I'd consider this wayyyy better than silently continuing with invalid value until the program tries something that doesn't make sense/is overreaching and it crashes.

      1 Reply Last reply
      1
      • A [email protected]

        “An abrupt exit”, more commonly known as a “crash”.

        If you’re going to argue that an exit through panic!() is not a crash, I will argue that your definition of a crash is just an abrupt exit initiated by the OS. In other words, there’s no meaningful distinction as the result is the same.

        Q This user is from outside of this forum
        Q This user is from outside of this forum
        [email protected]
        wrote last edited by [email protected]
        #16

        I don't think that's a valid comparison. The behavior does differ when it comes to cleanly releasing resources. Rust's panic performs the drop actions for the current values on the stack, a SIGILL or SIGSEGV crash doesn't.

        #[derive(Debug)]
        struct MyStruct {}
        
        impl Drop for MyStruct {
        	fn drop(&mut self) {
        		println!("{:?}", "imagine cleanup here"); // this is called
        	}
        }
        
        fn main() {
        	let a = MyStruct {};
        	panic!("panic!");
                println!("{a:?}");
        }
        

        Try it yourself

        A 1 Reply Last reply
        2
        • Q [email protected]

          I don't think that's a valid comparison. The behavior does differ when it comes to cleanly releasing resources. Rust's panic performs the drop actions for the current values on the stack, a SIGILL or SIGSEGV crash doesn't.

          #[derive(Debug)]
          struct MyStruct {}
          
          impl Drop for MyStruct {
          	fn drop(&mut self) {
          		println!("{:?}", "imagine cleanup here"); // this is called
          	}
          }
          
          fn main() {
          	let a = MyStruct {};
          	panic!("panic!");
                  println!("{a:?}");
          }
          

          Try it yourself

          A This user is from outside of this forum
          A This user is from outside of this forum
          [email protected]
          wrote last edited by
          #17

          That’s fair, although technically you could catch SIGSEGV and release resources that way too.

          Also, given that resources will be reclaimed by the OS regardless of which kind of crash we’re talking about, the effective difference is usually (but not always) negligible.

          Either way, no user would consider a panic!() to be not a crash because destructors ran. And most developers don’t either.

          1 Reply Last reply
          3
          • M [email protected]

            I prefer it over alternatives:

            • Exceptions: ”Oh no! Guess I’ll just die”
            • Error codes: ”If a non-zero error code is returned but no one notices, is it really an error?”
            F This user is from outside of this forum
            F This user is from outside of this forum
            [email protected]
            wrote last edited by
            #18

            The crash early, crash often approach of Erlang has made for some amazingly resilient systems.

            One time on a project I was working on, some horribly broken code was merged (nobody in the team had even heard of reviewing code). As soon as a specific call was made, it was executed once and then the thread crashed. The only way we noticed was that response times increased with load. All data and behavior was still correct. Whole nodes could go down and all you notice is a dip in performance until it comes back online.

            Of course it requires special care in designing. Everything runs in stateless server threads with supervisors restarting them as needed. This in turn requires some language support, like lightweight threads. Our application would happily run tens of thousands of threads on an ancient sparkstation.

            1 Reply Last reply
            2
            • J [email protected]
              try
              {
                   execute.SomeMethod();
              }
              catch(Exception ex)
              {}
              
              kiri@ani.socialK This user is from outside of this forum
              kiri@ani.socialK This user is from outside of this forum
              [email protected]
              wrote last edited by [email protected]
              #19

              goto fail;

              1 Reply Last reply
              0
              • korne127@lemmy.worldK [email protected]

                I mean using unwrap is not bad practice if the value is guaranteed to not be none, which can happen frequently in some applications.

                mobotsar@sh.itjust.worksM This user is from outside of this forum
                mobotsar@sh.itjust.worksM This user is from outside of this forum
                [email protected]
                wrote last edited by [email protected]
                #20

                If it's guaranteed to not be None, why is it an Option?

                E M korne127@lemmy.worldK 3 Replies Last reply
                2
                • mobotsar@sh.itjust.worksM [email protected]

                  If it's guaranteed to not be None, why is it an Option?

                  E This user is from outside of this forum
                  E This user is from outside of this forum
                  [email protected]
                  wrote last edited by
                  #21

                  Here's a bad example but hopefully captures the why. https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=43d055381e7bb52569c339d4526818f4

                  We have a condition we know must be satisfied (the option will always be Some), but cant prove in code.

                  1 Reply Last reply
                  1
                  • mobotsar@sh.itjust.worksM [email protected]

                    If it's guaranteed to not be None, why is it an Option?

                    M This user is from outside of this forum
                    M This user is from outside of this forum
                    [email protected]
                    wrote last edited by [email protected]
                    #22

                    Oh, it can happen when you do calculations with compile-time constants...

                    But the GP's claim that it's a "frequent" thing is suspect.

                    (Crashing is also useful when you are writing and-user applications, but you'll probably want .expect like in the meme.)

                    1 Reply Last reply
                    0
                    • A [email protected]

                      “An abrupt exit”, more commonly known as a “crash”.

                      If you’re going to argue that an exit through panic!() is not a crash, I will argue that your definition of a crash is just an abrupt exit initiated by the OS. In other words, there’s no meaningful distinction as the result is the same.

                      dgriffith@aussie.zoneD This user is from outside of this forum
                      dgriffith@aussie.zoneD This user is from outside of this forum
                      [email protected]
                      wrote last edited by [email protected]
                      #23

                      I was talking more about unwrap causing a panic rather than calling the actual panic macro directly. Rust forces the programmer to deal with bad or ambiguous results, and what that is exactly is entirely decided by the function you are calling. If a function decides to return None when (system timer mod 2 == 0), then you'd better check for None in your code. Edit: otherwise your code is ending now with a panic, as opposed to your code merrily trotting down the path of undefined behaviour and a segfault or similar later on.

                      Once you get to a point where we are doing the actual panic, well, that is starting to just be semantics.

                      1 Reply Last reply
                      1
                      • S [email protected]

                        Unwrap is good for prototyping and trying out stuff fast, but it generally shouldn't make it past a code review onto main, unless you're very sure

                        D This user is from outside of this forum
                        D This user is from outside of this forum
                        [email protected]
                        wrote last edited by
                        #24

                        Exactly.

                        Personally, I call it "python mode" since you're staying on the "happy path" and let the program just crash out if those expectations aren't met.

                        1 Reply Last reply
                        1
                        • mobotsar@sh.itjust.worksM [email protected]

                          If it's guaranteed to not be None, why is it an Option?

                          korne127@lemmy.worldK This user is from outside of this forum
                          korne127@lemmy.worldK This user is from outside of this forum
                          [email protected]
                          wrote last edited by
                          #25

                          A very typical use-case would be getting something from a HashMap (or a Vector) and calling unwrap because you know it must exist (as you got a reference to the index or object that must be valid in the HashMap or Vector).
                          Or if you call a function that returns Option<…> depending on the current state and you know that it must return Some(…) in the current situation.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups