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.
  • Q This user is from outside of this forum
    Q This user is from outside of this forum
    [email protected]
    wrote on last edited by [email protected]
    #1

    Don't say anyway, say anyhow

    S M sirico@feddit.ukS J 4 Replies Last reply
    151
    • Q [email protected]

      Don't say anyway, say anyhow

      S This user is from outside of this forum
      S This user is from outside of this forum
      [email protected]
      wrote on last edited by
      #2

      S T 2 Replies Last reply
      39
      • S [email protected]

        S This user is from outside of this forum
        S This user is from outside of this forum
        [email protected]
        wrote on last edited by
        #3

        Sad to be the one that does not get it.

        M ediacarium@feddit.orgE 2 Replies Last reply
        16
        • Q [email protected]

          Don't say anyway, say anyhow

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

          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 1 Reply Last reply
          11
          • Q [email protected]

            Don't say anyway, say anyhow

            sirico@feddit.ukS This user is from outside of this forum
            sirico@feddit.ukS This user is from outside of this forum
            [email protected]
            wrote on last edited by
            #5

            Elixir {:error, IDK lol}

            1 Reply Last reply
            5
            • S [email protected]

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

              every 20 lines.. haha that's so much.. visibly sweating

              1 Reply Last reply
              5
              • S [email protected]

                Sad to be the one that does not get it.

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

                Unwrap means it forces to evaluate the result as an ”ok value”. If it’s an ”error value”, it will crash. It’s a bad practice to rely on it, as it’s one of the most common ways a Rust programs can crash.

                Rust offers many options to handle errors that don’t risk crashing. For example, unwrap_or_default, which means ”if it’s an error value, use the default value for this type, such as 0 for integers”

                1 Reply Last reply
                31
                • S [email protected]

                  Sad to be the one that does not get it.

                  ediacarium@feddit.orgE This user is from outside of this forum
                  ediacarium@feddit.orgE This user is from outside of this forum
                  [email protected]
                  wrote on last edited by
                  #8

                  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.

                  dgriffith@aussie.zoneD T 2 Replies Last reply
                  11
                  • 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 on last edited by
                    #9

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

                    Q mobotsar@sh.itjust.worksM 2 Replies Last reply
                    11
                    • 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.

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

                      A good example would be regex. After validating it when writing the program it should always compile, although this could also be solved with a proc macro that validates the regex at compile time.

                      1 Reply Last reply
                      7
                      • 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.

                        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 on last edited by
                        #11

                        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 1 Reply Last reply
                        4
                        • Q [email protected]

                          Don't say anyway, say anyhow

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

                            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 1 Reply Last reply
                            6
                            • 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 on 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 on 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 on 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 on 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 on 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 on 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
                                          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