Checked vs unchecked exceptions

Moritz Maxeiner via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 6 06:16:23 PDT 2017


On Thursday, 6 July 2017 at 11:01:26 UTC, Marco Leise wrote:
> Am Thu, 06 Jul 2017 01:31:44 +0000
> schrieb Moritz Maxeiner <moritz at ucworks.org>:
>
>> But to be clear (and the title and description of any DIP
>> addressing this should reflect this):
>> These are not checked exceptions, because checked exceptions
>> would require bar to declare its exception set manually.
>
> Yep, absolutely clear. Just like "auto a = 1" does not declare
> a variable as we all know declarations start with a type.

Red herring.
Checked exceptions are well defined - not just implemented - (by 
Java as the de facto authority for the term) as requiring the 
declaration of all exceptions (that aren't inherited from some 
special class, in Java's case `RuntimeException`, in D's it would 
be `Error`) that may be thrown by a function as a result of its 
body being executed [1]. If even a single function is allowed to 
have its exception set defined by inference (instead of 
declaration), you are not implementing checked exceptions.

---
void foo() throws AExp throws BExc { ... }
void bar1() { foo(); } // Checked exceptions require this to 
result in a compilation error
void bar2() throws AExc throws BExc { foo(); } // this must be 
used for checked exceptions
---

> Instead of defining checked exceptions how it bests fits all
> your posts in this thread, why not say "Checked exceptions as
> implemented in Java were a good idea, had they allowed the
> compiler to infer them where possible."

Invalid premise. The definition of checked exceptions is de facto 
fixed by Java [1], because it not only coined the term but 
remains the only major PL to use them.

> Of course we can still call those inferred checked exceptions
> something else. ;o)

The java compiler does infer an exception set for every function 
based on its body.
This is then checked against the function's exception set as 
declared in its signature.
"inferred checked exceptions" is thus an oxymoron, because it 
would mean checking the inferred exception set against itself.

[1] 
https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html


More information about the Digitalmars-d mailing list