Why can't overriding methods throw exceptions broader than the overridden method?

前端 未结 15 1407
梦谈多话
梦谈多话 2020-11-22 13:47

I was going through SCJP 6 book by Kathe sierra and came across this explanations of throwing exceptions in overridden method. I quite didn\'t get it. Can any one explain it

15条回答
  •  醉酒成梦
    2020-11-22 14:35

    Rule of handling check and unchecked exceptions on overridden methods

    -When parent-class method declares no exception, then child-class overriding- method can declare,

     1. No exception or
     2. Any number of unchecked exception
     3. but strictly no checked exception
    

    -When parent-class method declares unchecked exception, then child-class overriding-method can declare,

     1. No exception or
     2. Any number of unchecked exception 
     3. but strictly no checked exception
    

    -When parent-class method declares checked exception, then child-class overriding-method can declare,

     1. No exception or
     2. Same checked exception or
     3. Sub-type of checked exception or
     4. any number of unchecked exception
    

    All above conclusion hold true, even if combination of both checked & unchecked exception is declared in parent-class’ method

    Ref

提交回复
热议问题