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.
Use J and K for navigation
Comments
32Comment deleted
C++ saw Java's Throwable hierarchy and replied with `throw whatever_compiles;`.
*** is obviously some unicode bullshit so the identifier is not void but void*** Comment deleted
ligatures maybe Comment deleted
Fira code Comment deleted
Why would you even throw a lambda?.. Comment deleted
Well, at least it's not a trigraph. 😌 https://en.wikipedia.org/wiki/Digraphs_and_trigraphs Comment deleted
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. Comment deleted
It'll be an exception with lambda stored in but not lambda itself Comment deleted
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. Comment deleted
Throwable is a class, but y're right in some point: it doesn't matter actually. Technically, lambda can be "transported" with throwing mechanism Comment deleted
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 Comment deleted
the difference is that in C++ you can throw anything, in Java you need to define it first Comment deleted
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. Comment deleted
developers like to write less, so there IS difference Comment deleted
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. Comment deleted
>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? Comment deleted
Run it the same way you'd run a runnable. Assuming that's your objective in throwing a lambda function. Comment deleted
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. Comment deleted
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. Comment deleted
>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++. Comment deleted
Callable exception 😎 Comment deleted
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. Comment deleted
that's not what "anything" is Comment deleted
Whatever dude. You're arguing for the sake of arguing. There's absolutely no substance. Comment deleted
That's wrong. It has to be Throwable. Exception is irrelevant. Comment deleted
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. Comment deleted
indeed but it doesn't "have to be wrapped with an Exception" Comment deleted
☝️C++ guy won i guess. I’ll go with C++ in my next project Comment deleted
Try Rust Comment deleted
No, thanks, i take showers Comment deleted
I do too, at least once a month! /j Comment deleted
but you have no legs Comment deleted