java - Why is it that an overridden method can't throw a new checked Exception -
this question has answer here:
i have 2 questions :
- what purpose of constraint overridden method can't throw new checked exception?
- why allowed overridden method can throw or none, or subset of checked exceptions specified in throws clause of overridden method in superclass?
in both cases, it's because base method you're overriding has set contract calling code; if add checked exceptions method may throw, you'd breaking contract.
consider class base
method foo
throws checked exception someexception
. have derived
derives base
, overrides foo
. code in app
using base b
variable initializing new instance of derived
, , calling b.foo()
. contract foo
throws someexception
; throwing else breaks contract.
Comments
Post a Comment