📜 ⬆️ ⬇️

Overview of the restrictions imposed on exceptions, for example, inheritance and interface implementation

Greetings to all Java students!
As it is known, exceptions in Java, when inheriting, obey the requirement of narrowing the specification, and not its extensions, unlike normal classes.
Bruce Eckel's book “Thinking in Java, 4 ed.” Describes the limitations of using exceptions. The sample code describes the inheritance from the base class and the simultaneous implementation of the interface, and both have methods with the same singatura, but different specification of exceptions. Attempts to implement interface methods overlap with attempts to simultaneously redefine the methods of the base class, as a result of which emphasis is placed on preventing the extension of the specification of exceptions. A simplified example code that conveys the idea:

class BaseBallException extends Exception{} class Foul extends BaseBallException{} class Strike extends BaseBallException{} abstract class Inning { //-----Methods---- public Inning() throws BaseBallException{} public void event() throws BaseBallException{} public abstract void atBat() throws Strike, Foul; } class StormException extends Exception{} class RainedOut extends StormException{} class PopFoul extends Foul{} interface Storm { public void event() throws RainedOut; } class StormyInning extends Inning implements Storm { //--------Methods----- public StormyInning() throws RainedOut, BaseBallException{} public StormyInning(String str) throws Foul, BaseBallException{} /**  -   Storm.event() throws RainedOut    *   Inning.event() throws BaseBallException    RainedOut*/ //! public void event() throws RainedOut{} /**   event()     -    *        . *    -  */ public void event() {} public void atBat() throws PopFoul {} } 


The example can be supplemented with other variants of exceptions specifications, sorting through some interesting combinations:
')
 class BaseBallException extends Exception{} class Foul extends BaseBallException{} class Strike extends BaseBallException{} class LightStrike extends Strike{} class TwistedLightStrike extends LightStrike{} abstract class Inning { //-----Methods---- public Inning() throws BaseBallException{} public void event() throws BaseBallException{} public void event1() throws BaseBallException{} public void event2() {} public void event3() throws RainedOut{} public void event4() throws Strike{} public void event5() throws LightStrike{} } class StormException extends Exception{} class RainedOut extends StormException{} class Drizzle extends RainedOut{} class PopFoul extends Foul{} interface Storm { public void event() throws RainedOut; public void event1(); public void event2() throws RainedOut; public void event3() throws RainedOut; public void event4() throws LightStrike; public void event5() throws Strike; } class StormyInning extends Inning implements Storm { //--------Methods----- public StormyInning() throws RainedOut, BaseBallException{} public StormyInning(String str) throws Foul, BaseBallException{} /**  -   Storm.event() throws RainedOut    *   Inning.event() throws BaseBallException    RainedOut*/ //! public void event() throws RainedOut{} /**  -   Storm.event() throws RainedOut c   RainedOut  * BaseBallException       */ //! public void event() throws BaseBallException{} /**  -          *  */ //! public void event() throws RainedOut, BaseBallException{} /**   */ public void event() {} /**  -   Storm.event1()    */ //! public void event1() throws BaseBallException {} /**    -     Storm.event1()  *    */ public void event1(){} /**  -     Inning.event()   * */ //! public void event2() throws RainedOut{} /**       . *     ..       * Storm.event2() throws RainedOut    */ //public void event2(){} /**  Storm.event3() throws RainedOut    -   *     Storm.event3() throws RainedOut,   * .         */ //public void event3() throws RainedOut{} /**           *      */ //public void event3() throws Drizzle{} //public void event3() {} /**  ,       event4()*/ /**  event4()  ..    ,    *  */ /**  -       Storm.event4()*/ //! public void event4() throws Strike{} /** -     ,  - */ //public void event4() throws LightStrike{} /** -       */ //public void event4() throws TwistedLightStrike{} public void event4() {} /**    event4(),        -     *    */ //! public void event5() throws Strike{} //public void event5() throws LightStrike{} //public void event5() throws TwistedLightStrike{} //public void event5() } 


Obviously, the above limitations and features will not be superfluous to consider when designing a class hierarchy for the next project.

Source: https://habr.com/ru/post/178477/


All Articles