D features

Marco Leise Marco.Leise at gmx.de
Thu Sep 22 04:04:40 PDT 2011


Am 21.09.2011, 01:20 Uhr, schrieb Walter Bright  
<newshound2 at digitalmars.com>:

> On 9/20/2011 2:28 PM, Jonathan M Davis wrote:
>>> On Tue, 20 Sep 2011 15:58:06 -0400, Rishat Galiulin
>>>> 2) Why D not using functions exceptions specification list like Java?
>> Checked exceptions have generally been determined to have been a bad  
>> idea.
>
>
> Bruce Eckel's article about it convinced me.
>
> http://www.mindview.net/Etc/Discussions/CheckedExceptions

I don't find this article all that convincing. The symptom is that people  
begin to write stub exception handlers and 'swallow' the exception. And  
this is seen as proof of a failed concept. I wrote exception stubs myself  
in the past. Mainly because I had to compile the program first to see the  
error, then write the proper exception handler or write a throw clause.  
That is cumbersome and you tend to just write a stub at the lowest level  
in your code, because it is less typing.

Checked exceptions have the purpose to inform the program of invalid  
conditions in areas outside the immediate control of the program. In a  
released program you should take care of those one way or another, because  
it is likely that a host name cannot be resolved, or a file cannot be read  
(down to the fact that the disk has bad sectors). So you are made aware of  
this and must solve the problem in one of three ways, depending on the  
code structure:
- add a throws clause to let the next higher level handle it
- catch and handle it right there
- do both
- wrap the exception (if it happened in the subprocess of something bigger)
It is a bit of a motivation to write actual error handlers, that you can  
easily forget otherwise.

One of the reasons Java is popular is because of the great tools support.  
I am a big fan of Eclipse and if you have a look at the situation there  
you notice

1. try {...} catch (SomeKindOfException e) {}
    will mark the empty braces and print a warning about an undocumented  
empty block
2. Uncaught checked exceptions will show up as an error while you are  
typing
3. For these errors and many others there are 'quick-fixes'. You just  
press Ctrl+1 and choose
    a) add throws clause to method
    b) surround by try-catch

3b will add a stub like this:
} catch (ExceptionType e) {
   e.printStackTrace();
}

This means that no Exception actually goes unnoticed anymore as described  
in the article! It is plain no point today in Java. :)
I guess one can say "they hack around the flaws in the language, you need  
an IDE to even use it properly" or one can say "they learned and found a  
way to make a good idea just work". For D there is no such IDE and all  
language features must promote good code when written in programming  
editors, Java can excuse a lot in that aspect.


More information about the Digitalmars-d mailing list