Skip to content
DevMeme
4612 of 7590
Languages Post #5060 · source on Telegram

Java Panics at C++ Exceptions

Description

A two-panel Wojak-versus-Chad meme labels the crying figure on the left as "JAVA" and the calm bearded figure on the right as "C++". The Java side says, "Nooo! You can't just take something and throw it as an exception!" while the C++ side shows a dark code snippet: `void foo() { // ... throw ([&](void***){}); }`. The joke contrasts Java's constrained exception hierarchy with C++ exception semantics, where almost any copyable object can be thrown, including a grotesque-looking lambda type.

Comments

32
Anonymous ★ Top Pick C++ saw Java's Throwable hierarchy and replied with `throw whatever_compiles;`.
  1. Anonymous ★ Top Pick

    C++ saw Java's Throwable hierarchy and replied with `throw whatever_compiles;`.

  2. @beton_kruglosu_totchno 3y

    *** is obviously some unicode bullshit so the identifier is not void but void***

    1. @sylfn 3y

      ligatures maybe

    2. @azizhakberdiev 3y

      Fira code

  3. @VanuxaKR 3y

    Why would you even throw a lambda?..

  4. @SamsonovAnton 3y

    Well, at least it's not a trigraph. 😌 https://en.wikipedia.org/wiki/Digraphs_and_trigraphs

  5. @LonelyGayTiger 3y

    I dont see why you couldnt throw a lambda in Java. Exceptions are just objects like everything else and they can store any other object if extended to do so.

    1. @eternal_neophyte 3y

      It'll be an exception with lambda stored in but not lambda itself

      1. @LonelyGayTiger 3y

        You could build the exception object with a lambda. I dont know that the distinction actually matters. It can be any object that implements throwable, which you can make any lambda object do.

        1. @eternal_neophyte 3y

          Throwable is a class, but y're right in some point: it doesn't matter actually. Technically, lambda can be "transported" with throwing mechanism

        2. @beton_kruglosu_totchno 3y

          in C++ you do not need to build anything you throw any Shit and you catch any Shit In Java you get advanced stacktraces thanks to that Exception base object, C++ simply does not give a shit

        3. @beton_kruglosu_totchno 3y

          the difference is that in C++ you can throw anything, in Java you need to define it first

          1. @LonelyGayTiger 3y

            You can define it in a lambda, from a developers perspective it doesnt look much different from how you'd use a lambda in C++. It may be technically different, but functionally there isnt a significant difference.

            1. @beton_kruglosu_totchno 3y

              developers like to write less, so there IS difference

              1. @LonelyGayTiger 3y

                If you're already a java dev the minor difference in amount of code is going to be pretty negligible. You're not going to decide on a language to build a project in based on the amount of code it takes to effectively throw a lambda function.

            2. @beton_kruglosu_totchno 3y

              >You can define it in a lambda and then you do what with it when you catch a Throwable or an Exception which it extends?

              1. @LonelyGayTiger 3y

                Run it the same way you'd run a runnable. Assuming that's your objective in throwing a lambda function.

                1. @beton_kruglosu_totchno 3y

                  funny how you concentrate on the lambdas because the only reason you can throw them and then use the thrown value is because of under the hood JVM reflection. I said you can throw anything in C++. You did not upset my argument at all. For any value you throw the catching code either needs to have an external definition of it or rely on JVM reflection and even then you need to define this value as extension of Throwable or Exception.

                  1. @LonelyGayTiger 3y

                    The example was a lambda function and that's what I was responding to. The fact that you have to use JVM reflection is irrelevant. Everything's an object in Java, any object can be stored in an Exception, so with a little work you can throw anything you want. If the goal is to throw a lambda function, as shown in the example, it can be done in Java. How is irrelevant.

                    1. @beton_kruglosu_totchno 3y

                      >The example was a lambda function and that's what I was responding to. You were responding to my message where I said that you can throw anything in C++.

  6. @Vlasoov 3y

    Callable exception 😎

  7. @LonelyGayTiger 3y

    Originally I the example is a lambda function. But if you really want to get into semantics, as everything in Java is an object, you can throw anything in Java. It just has to be wrapped with an Exception. How exactly it works is irrelevant.

    1. @beton_kruglosu_totchno 3y

      that's not what "anything" is

      1. @LonelyGayTiger 3y

        Whatever dude. You're arguing for the sake of arguing. There's absolutely no substance.

    2. Deleted Account 3y

      That's wrong. It has to be Throwable. Exception is irrelevant.

      1. @LonelyGayTiger 3y

        I'm aware. An Exception is Throwable. So if it's Wrapped with an Exception it'll work. There are reasons you might prefer to use an Exception rather than just extending Throwable directly in this kind of use case.

        1. Deleted Account 3y

          indeed but it doesn't "have to be wrapped with an Exception"

  8. @grygree 3y

    ☝️C++ guy won i guess. I’ll go with C++ in my next project

    1. @mekosko 3y

      Try Rust

      1. @grygree 3y

        No, thanks, i take showers

        1. @RiedleroD 3y

          I do too, at least once a month! /j

        2. Deleted Account 3y

          but you have no legs

Use J and K for navigation