5 ms·
Nice to see pragmatism playing a role here. A big problem with Java has always been the "design by committee" nature of it where we end up with some theoretica
by zmmmmm 15y ago
Nice to see pragmatism playing a role here. A big problem with Java has always been the "design by committee" nature of it where we end up with some theoretical best case that satisfies everyone's egos but turns out to utterly suck when used in practice.
From another point of view ... it's a shame we needed so many years to come up with "let's do it like C#".
- bad_user 15y agoIt's also a shame they haven't done so with other features -- like getting rid of checked exceptions.
- reinhardt 15y agoGetting rid of something is always harder than adding it later.
- loup-vaillant 15y agoI don't think this is the case here. The question isn't whether you "add" or "remove" something. The question is whether the old programs will still work or not. If I understand checked exceptions correctly, removing them will only allow more programs to compile. The old ones will still be valid as their (now optional) try-catch/finally blocks won't go anywhere.
- Torn 15y agothrows RuntimeException and try / catch around RuntimeException would still be valid, so I can't see how changing all checked exceptions to runtime shouldn't affect the flow of the code. If users wanted to they could declare 'throws TheirParticularRuntime' all the way up the stack and it would work like checked exceptions, right?
- xtracto 15y agoCan you expand on why would you like to get rid of checked exceptions?
- grahamedgecombe 15y agoIt often leads people to write code like this: try { // do stuff } catch (TheCheckedException ex) { // ignore it } then the code continues even though it could be in a bad state if an exception did occur. It's a much better solution to let it propagate up the call stack until it can be dealt with appropriately (e.g. by ending the application, giving a 503 error on a web page request, etc.) Unchecked exceptions will propagate up the call stack by default if you don't do anything, making it harder for people to write bad exception handling code.
- Sandman 15y agoSo add a 'throws' clause to the method in which you call the code that throws the checked exception and it will propagate upward.
- samuel 15y ago...or rethrow it as an unchecked exception(RuntimeException).
- brlewis 15y agoYes, this! The most irritating debugging I've had to do in Java code is when someone catches an exception, but fails to handle it effectively, resulting in another exception being thrown later. If you don't know how to handle it effectively, just add the 'throws' clause. It's less typing than try/catch, so I don't understand why people choose to make extra work for themselves and for me. I see a lot of comments here that seem to assume catching the exception is the only option.
- davedx 15y agoExactly. In Eclipse it's so ridiculously easy to make the method pass it on by adding the throw to your method. I find checked exceptions really useful every day :)
- wlievens 15y agoSome people like them, you know.
- vetler 15y agoAt least Java 7 has multi-catch, i.e. catch (FirstException | SecondException ex)
- wcoenen 15y agoI think checked exceptions are just ahead of their time. They make sense when you statically check your code against contracts, which is why they showed up again in the spec# research project. http://en.wikipedia.org/wiki/Spec_Sharp http://en.wikipedia.org/wiki/Spec_Sharp
- nl 15y agoOptional checked exceptions have some benefits. Java got them the wrong way around - unchecked should have been the default, with checked available when you need them. Because Java does them the other way around, you either get code bloat from the large numbers of exceptions you are catching and handling subtly differently or you have a generic catch(Exception e){..} block. Neither are ideal.
- famousactress 15y agoexactly right. I'm always surprised more people don't land here when this debate comes up.
- paperwork 15y agoWhy are you opposed to checked exceptions? Frankly this is one complaint about java I have never understood. I, myself, have missed checked exceptions in C# when I have had to port Java code to C#.