FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/exceptions/StormyInning.java at master · CSapprenticeWT/OnJava8-Examples · GitHub
CSapprenticeWT
OnJava8-Examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
exceptions
/
StormyInning.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
80 lines (75 loc) · 2.65 KB
Breadcrumbs
OnJava8-Examples
/
exceptions
/
StormyInning.java
Copy path
File metadata and controls
80 lines (75 loc) · 2.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// exceptions/StormyInning.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Overridden methods can throw only the exceptions
// specified in their base-class versions, or exceptions
// derived from the base-class exceptions
class
BaseballException
extends
Exception
{}
class
Foul
extends
BaseballException
{}
class
Strike
extends
BaseballException
{}
abstract
class
Inning
{
Inning
()
throws
BaseballException
{}
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
;
}
public
class
StormyInning
extends
Inning
implements
Storm
{
// 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
{}
// Regular methods must conform to base class:
//- void walk() throws PopFoul {} //Compile error
// Interface CANNOT add exceptions to existing
// methods from the base class:
//- public void event() throws RainedOut {}
// If the method doesn't already exist in the
// base class, the exception is OK:
@
Override
public
void
rainHard
()
throws
RainedOut
{}
// You can choose to not throw any exceptions,
// even if the base version does:
@
Override
public
void
event
() {}
// Overridden methods can throw inherited exceptions:
@
Override
public
void
atBat
()
throws
PopFoul
{}
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"
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL