• stetech@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    2 days ago

    C# is basically Java and from what I can tell, this looks approximately valid.

    Variables can always* be named freely to your liking.

    *You used to have to stick to the Latin alphabet, but that’s increasingly not the case anymore. Emoji-named variables FTW!

    • jaybone@lemmy.world
      link
      fedilink
      arrow-up
      1
      arrow-down
      1
      ·
      2 days ago

      No it’s not “basically Java”

      Aside from how Microsoft stole it, fucked the standard library, fucked the naming conventions, etc. You would never just “throw” without specifying what you were throwing.

      • CodexArcanum@lemmy.dbzer0.com
        link
        fedilink
        English
        arrow-up
        10
        ·
        2 days ago

        This is incorrect. The C# is valid. Throw in a catch statement simply rethrows the caught exception. Source: I’ve been writing C# for 20 years, also the docs.

        I won’t act like MS absolutely didn’t steal core concepts and syntax from Java, but I’ve always thought C# was much more thoughtfully designed. Anders Hejlsberg is a good language designer, TypeScript is also a really excellent language.

        • jaybone@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          2 days ago

          In Java you would say “throw e;” (to rethrow the same exception you just caught.)

          You wouldn’t just say “throw”

          Or you could also throw some other exception. But the syntax requires you specify what it is you are throwing. (And sane in C++, where you could throw any object, even a primitive.)

          So that was my question.

          • CodexArcanum@lemmy.dbzer0.com
            link
            fedilink
            English
            arrow-up
            5
            ·
            2 days ago

            Wildly, in C# you can do either and it has different results. I believe a bare throw doesn’t append to the stack trace, it keeps the original trace intact, while throw e updates the stack trace (stored on the exception object) with the catch and rethrow.

            In C#, you can only throw objects whose class derives from Exception.

            • Hoimo@ani.social
              link
              fedilink
              arrow-up
              1
              ·
              2 days ago

              You can do either, but you usually do neither. The best way is to throw a new exception for your situation and add the caught exception as an inner exception. Because rethrowing resets the stack trace, removing the context from an exception message that is often pretty vague, and “bouncing” with throw; doesn’t tell the next exception handler that you already handled (part of) the exception.

          • dependencyinjection@discuss.tchncs.de
            link
            fedilink
            arrow-up
            2
            ·
            2 days ago

            To be honest I’m just playing into the meme of Java.

            My understanding is it’s academically great, but a pain in practice.

            For reference we use C# .Net, Entity Framework with GraphQL and React TypeScript for our enterprise applications and I really like C# now, but when I first started I’d only really used Node.js and some Java.