News for hackers. Stuff that matters.
Re:JEP draft: Exception handling in switch by Groxx on Friday April 19, 2024 @01:39PM (#40089762)
> case throws Exception e -> { log(e); yield score(0); }

... and this is why people so frequently break thread interrupts in Java. most Java sample code (and even official-feeling docs like this) violates basic exception hygiene.

---

edit: that aside, I can see lots of uses for this pattern, and I like the clear scoping quite a bit. Seems like a good idea on the surface at the very least - I don't have enough experience here to really make a "good idea or no" claim.

Re:JEP draft: Exception handling in switch by gizmo686 on Friday April 19, 2024 @02:10PM (#40090153)
This complaint is not really about the proposed change, it is a longstanding problem with Java. As long as you treat exceptions with the sane care in switch statements as you do in try statements, this doesn't introduce any new problems.
Re:JEP draft: Exception handling in switch by bedobi on Friday April 19, 2024 @02:33PM (#40090418)
> it is a longstanding problem with Java

it's, along with null pointers, probably the no 1 biggest foot gun in the language, responsible for countless bugs in pretty much every app ever written in the language, and it's treated as some unaddressed elephant in the room we don't talk about or consider alternatives to

Re:JEP draft: Exception handling in switch by dgellow on Friday April 19, 2024 @03:55PM (#40091303)
C# added nullable reference types a few years ago. Is there an equivalent in Java? That makes dealing with null pretty much a non-issue

https://learn.microsoft.com/en-us/dotnet/csharp/nullable-ref...

Re:JEP draft: Exception handling in switch by PaulHoule on Friday April 19, 2024 @02:59PM (#40090732)
It is easy to put the right behavior in a function wrapper like

https://github.com/paulhoule/pidove/blob/97f8ec697d2890f13ba...

Re:JEP draft: Exception handling in switch by grishka on Friday April 19, 2024 @05:24PM (#40092065)
> and this is why people so frequently break thread interrupts in Java.

By the way. I've been writing predominantly Java over the last 15 years and I absolutely hate it that InterruptedException is checked. It gets in the way All. The. Damn. Time. All for those 0.1% of cases when you need to be able to cancel a blocking operation from another thread.