Live data from Hacker News

Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

slideshare.net

11–20 of 72 posts

Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

#11
post #2

Interesting how java is the only language included in the title, but the slides have the opinion that C#, Ruby and Python all suck as well. Seems like a cheap way to get upvotes.

And in python you'd use the with construct.

Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

#12

I think you can avoid nesting finally thus: X x = null; Y y = null; try{ x = foo(); y = bar(); yadda(x,y); } finally { if (x!=null) x.dispose(); if (y!=null) y.dispose(); }

what happens if x.dispose() throws an exception?

if (x != null) try { x.dipose(); } catch (Exception){} }

it's why the using keyword is so nice. Still, this is all hoops languages force us to deal with when they shouldn't (which is the OPs point)

Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

#13
post #8
post #2

Interesting how java is the only language included in the title, but the slides have the opinion that C#, Ruby and Python all suck as well. Seems like a cheap way to get upvotes.

He sort of glosses over the "syntatic sugar" in C#. Which is simply: using(var resource = new Resource()) { // potential code that throws exception }

Java finally (pun intended) gets a similar feature in Java 7 later this year: try-with-resources:

http://download.java.net/jdk7/docs/technotes/guides/language...

Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

#14
post #7

...am I reading correctly that his argument for using C++/D (!) is that it's hard to remember to say this: try { mightFail(); } finally { doCleanup(); } instead of this: mightFail(); doCleanup();

That seems like a pretty serious argument to me. Not only is it a pain in the ass to remember and write that every time you consume a resource that needs to be released somehow, it also introduces all kinds of scoping headaches in Java. If mightFail() returns a value that you want to use after cleanup (i.e. all the time,) then you have to declare the variable to store that value outside of the try {} block.

Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

#15
post #13
post #8

Earlier quoted context omitted.

He sort of glosses over the "syntatic sugar" in C#. Which is simply: using(var resource = new Resource()) { // potential code that throws exception }

Java finally (pun intended) gets a similar feature in Java 7 later this year: try-with-resources: http://download.java.net/jdk7/docs/technotes/guides/language...

It's unclear to me why he says that the JDK7 addition doesn't really solve the problem. It seems to me that it is exactly this problem that ARM blocks are trying to solve. It's still not as clean as leveraging deconstructors when an object goes out of scope but it's much better than before.

Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

#16
post #10
post #5

Earlier quoted context omitted.

And there actually is an idiomatic way to avoid the problem in Ruby: File.open("...") do |f| firstline = f.readline ... stuff that might throw an exception ... end If the "stuff" throws an exception, the file gets closed automatically. And while File is a library class, it's getting no special favors here --- any pure ruby library can easily implement similar APIs, and ActiveRecord's connection pool, for example, act…

Which counts as the sugar which he mentions. See also "using" in C#. These are pretty clearly design warts. WPF (C# UI library) jumps through some interesting hoops to make it look like all your resources can be properly garbage collected, but even then it tends to come back and bite you for any non-trivial application.

It doesn't really taste like syntactic sugar though; it's a special functionality of the standard library, and you could easily get away without knowing about the Java/C-style API of keeping a file handle that has to be controlled around.

Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

#17
post #12

I think you can avoid nesting finally thus: X x = null; Y y = null; try{ x = foo(); y = bar(); yadda(x,y); } finally { if (x!=null) x.dispose(); if (y!=null) y.dispose(); }

what happens if x.dispose() throws an exception? if (x != null) try { x.dipose(); } catch (Exception){} } it's why the using keyword is so nice. Still, this is all hoops languages force us to deal with when they shouldn't (which is the OPs point)

You're lucky Java is checked at compile time. Ruby would eat that "no such method dipose" and silently leak x.

Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

#18
post #13

Earlier quoted context omitted.

Java finally (pun intended) gets a similar feature in Java 7 later this year: try-with-resources: http://download.java.net/jdk7/docs/technotes/guides/language...

It's unclear to me why he says that the JDK7 addition doesn't really solve the problem. It seems to me that it is exactly this problem that ARM blocks are trying to solve. It's still not as clean as leveraging deconstructors when an object goes out of scope but it's much better than before.

Well, it's an awkward solution, because exceptions have this general problem of blowing away everything you're doing, and it's only a solution for this one case.

The solution to this problem that makes sense to me is either conditions and restarts a la Common Lisp, or a type system that can handle multiple return values of different types in a sane way (e.g. return either a result or an error code and then pattern match against them) so that you don't always need to throw an exception in order to deal with an error.

Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

#19
post #7

...am I reading correctly that his argument for using C++/D (!) is that it's hard to remember to say this: try { mightFail(); } finally { doCleanup(); } instead of this: mightFail(); doCleanup();

No, you're just oversimplifying his argument. It's not only "hard to remember", it also makes your code overly verbose. That, in turn, degrades its readability, making everyone else spend more time on it and making you, as the author, avoid using the pattern, which leads to writing "prettier", yet unsafe code. Then again, if "hard to remember" is meant as a euphemism for "I know I should write things this way but I really don't want to", then you're completely right.

All in all, he's arguing for RAII, which is impossible in Java and a bunch of other popular languages.

Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)

#20
post #12

Earlier quoted context omitted.

what happens if x.dispose() throws an exception? if (x != null) try { x.dipose(); } catch (Exception){} } it's why the using keyword is so nice. Still, this is all hoops languages force us to deal with when they shouldn't (which is the OPs point)

You're lucky Java is checked at compile time. Ruby would eat that "no such method dipose" and silently leak x.

You're lucky Java is checked at compile time.

Not lucky enough, because x.dispose() could throw an unchecked exception.

Post reply on HN