Live data from Hacker News

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

slideshare.net

61–70 of 72 posts

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

#61

Python's "with" statement handles this scenario: http://effbot.org/zone/python-with-statement.htm

The author is aware of that, but you have to remember to use "with". If you forget to use it, you can still have unclaimed resources.

You also need to remember to use RAII, and that always requires creating a class. You also need to get RAII right, where as Python's with is implemented for you.

Also there's no mention about the fact that Java forces you to check for exceptions (well not for unchecked ones, but those should not be fatal, and I never really got their idea). You can't forget the try-catch.

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

#62
This fact has lead to the popularity of the springframework in Java. They use the template design pattern to hide all of the resource acquisition and release. This makes it much easier to code as you don't have to "remember" to close your db connections. The remember argument is somewhat weak because you still need to remember to write your destructor. I do buy that it's easier to remember it in one place than all over the code.

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

#63
post #61

Earlier quoted context omitted.

The author is aware of that, but you have to remember to use "with". If you forget to use it, you can still have unclaimed resources.

You also need to remember to use RAII, and that always requires creating a class. You also need to get RAII right, where as Python's with is implemented for you. Also there's no mention about the fact that Java forces you to check for exceptions (well not for unchecked ones, but those should not be fatal, and I never really got their idea). You can't forget the try-catch.

The library author needs to remember to use RAII, but the client doesn't need to remember anything (as in his examples). Whereas with 'with' it's the opposite - the client has to remember to use it.

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

#64

I am not super familiar with all the details of Java inner classes, but why can't you get most of the way there by doing something like: DB.open(new Runnable() { public void run() { // ... do stuff here ... } }); You can even design your resource layers such that they can only be used this way (or are easiest to use this way). Basically, I'm just stealing the JavaScript-y way of doing this that uses closures: DB.open…

Answer: the inner class can only access constant locals :(. So you have to put any mutable state in members.

Blech. This might work for some cases, but would be a huge pain in the ass in others.

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

#66

The majority of the comments seem to be negative, yet many of them reflect a misunderstanding of RAII. I've argued the exact argument the author does many times before and got similar responses, it seems to be hard to convey the power of RAII to people who haven't practiced it before. The meaning of RAII is that you can tie a resource to the lifetime of an object, in an environment where the object gets destroyed det…

Amen.

RAII will be in the next great language, as it is a useful tool. This is not an academic concern, it is something that happens all the time due to rushed deadlines, stressed developers, or simple naivete.

People love to slag off C++ but the higher-level devs have done some serious thinking about how to engineer robust programs. Sutter's 'Exceptional C++' is eye-opening the first time around, and the concepts are applicable to any language that has exception handling. Programming in a transactional manner has visibly improved my designs -- mostly through the paranoia that almost any statement could throw an exception.

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

#67
post #60
post #57

Earlier quoted context omitted.

Likewise for auto_ptr , but he glosses over that.

How do you figure? There isn't an implicit conversion from auto_ptr to T *.

There isn't. There are two explicit methods to get raw pointer value with different semantics:

- get(): auto_ptr retains the ownership

- release(): auto_ptr loses the ownership

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

#68
post #63
post #61

Earlier quoted context omitted.

You also need to remember to use RAII, and that always requires creating a class. You also need to get RAII right, where as Python's with is implemented for you. Also there's no mention about the fact that Java forces you to check for exceptions (well not for unchecked ones, but those should not be fatal, and I never really got their idea). You can't forget the try-catch.

The library author needs to remember to use RAII, but the client doesn't need to remember anything (as in his examples). Whereas with 'with' it's the opposite - the client has to remember to use it.

[deleted]

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

#69
post #40
post #33

Earlier quoted context omitted.

You seem to have failed to grasp that the slides are arguing for RAII. While admittedly mildly trollish, it disparages java along with C#/ruby/python implicitly in favour of C++ only because C++ has the most 'correct' implementation of RAII. There is no argument against try/catch/finally, the argument is that the most common usage of try blocks is for dealing with resource management, not actual exceptions in program…

And RAII in C++ relies on programmer acceptance of that idiom.

Arguably it relies on the acceptance of fewer programmers, the library writers.

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

#70

What you really want is something along the lines of: void myMethod() { disposable File myFile = new File(somePath); // ... do stuff with the file // "disposable" modifier causes myFile to be // forcibly destroyed upon leaving scope for any // reason (except if the disposable object itself // is returned from the method). } An idiom designed specifically for the purpose of resource management would make for a far cle…

Isn't that exactly what reference counting ala shared_ptr does?
Post reply on HN