Checked vs unchecked exceptions
Moritz Maxeiner via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jun 26 10:43:08 PDT 2017
On Monday, 26 June 2017 at 15:40:19 UTC, mckoder wrote:
> On Monday, 26 June 2017 at 15:15:54 UTC, Steven Schveighoffer
> wrote:
>>
>> No, checked exceptions leads to this (maybe not for you, but
>> for 90% of developers out there):
>>
>> void foo()
>> {
>> functionWithException();
>> }
>>
>> compiler: foo throws, and you need to handle or declare the
>> exceptions it throws
>>
>> void foo()
>> {
>> try {
>> functionWithException();
>> } catch(Exception e) {} // shut up compiler
>> }
>>
>
> Why wouldn't you instead write:
>
> void foo() throws Exception // shut up compiler
> {
> functionWithException();
> }
>
> That's easier, and no worse than C# even though you have
> defeated checked exceptions.
Because that's even worse.
The `try catch` as least takes responsibility for handling the
error - even if it is just intentionally ignoring it.
Your version delegates the responsibility up the call chain
(which is in and of itself fine), but it - as you even stated -
prevents the caller from using checked exceptions, i.e. you're
cheating the system.
> Here's the point: with checked exceptions good programmers can
> write good code.
With checked exceptions any programmer is forced to
a) annotate every single function with the complete aggregate of
the exceptions that may be thrown by itself or the functions it
calls
b) violate checked exceptions and limit its callers by marking
itself as throwing a parent exception
Or, more succinct: You must either manually write things down the
compiler could find out in a fraction of the time via static
analysis, or cheat the system; both cases are bad code.
More information about the Digitalmars-d
mailing list