OnJava8-Examples/exceptions/StormyInning.java

81 lines
2.7 KiB
Java
Raw Permalink Normal View History

2015-09-07 11:44:36 -06:00
// exceptions/StormyInning.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
// Overridden methods can throw only the exceptions
// specified in their base-class versions, or exceptions
2016-01-25 18:05:55 -08:00
// derived from the base-class exceptions
2015-06-15 17:47:35 -07:00
class BaseballException extends Exception {}
class Foul extends BaseballException {}
class Strike extends BaseballException {}
abstract class Inning {
2017-05-01 14:33:10 -06:00
Inning() throws BaseballException {}
2015-06-15 17:47:35 -07:00
public void event() throws BaseballException {
// Doesn't actually have to throw anything
}
public abstract void atBat() throws Strike, Foul;
public void walk() {} // Throws no checked exceptions
}
class StormException extends Exception {}
class RainedOut extends StormException {}
class PopFoul extends Foul {}
interface Storm {
void event() throws RainedOut;
void rainHard() throws RainedOut;
2015-06-15 17:47:35 -07:00
}
2015-12-15 11:47:04 -08:00
public
class StormyInning extends Inning implements Storm {
2015-06-15 17:47:35 -07:00
// OK to add new exceptions for constructors, but you
// must deal with the base constructor exceptions:
public StormyInning()
throws RainedOut, BaseballException {}
public StormyInning(String s)
throws BaseballException {}
2015-06-15 17:47:35 -07:00
// Regular methods must conform to base class:
2015-12-18 11:28:19 -08:00
//- void walk() throws PopFoul {} //Compile error
2015-06-15 17:47:35 -07:00
// Interface CANNOT add exceptions to existing
// methods from the base class:
2015-12-18 11:28:19 -08:00
//- public void event() throws RainedOut {}
2015-06-15 17:47:35 -07:00
// If the method doesn't already exist in the
// base class, the exception is OK:
@Override public void rainHard() throws RainedOut {}
2015-06-15 17:47:35 -07:00
// You can choose to not throw any exceptions,
// even if the base version does:
@Override public void event() {}
2015-06-15 17:47:35 -07:00
// Overridden methods can throw inherited exceptions:
@Override public void atBat() throws PopFoul {}
2015-06-15 17:47:35 -07:00
public static void main(String[] args) {
try {
StormyInning si = new StormyInning();
si.atBat();
} catch(PopFoul e) {
System.out.println("Pop foul");
} catch(RainedOut e) {
System.out.println("Rained out");
} catch(BaseballException e) {
System.out.println("Generic baseball exception");
}
// Strike not thrown in derived version.
try {
// What happens if you upcast?
Inning i = new StormyInning();
i.atBat();
// You must catch the exceptions from the
// base-class version of the method:
} catch(Strike e) {
System.out.println("Strike");
} catch(Foul e) {
System.out.println("Foul");
} catch(RainedOut e) {
System.out.println("Rained out");
} catch(BaseballException e) {
System.out.println("Generic baseball exception");
}
}
2015-09-07 11:44:36 -06:00
}