package
com.zj.exception.types;
public
class
NotBigEnoughException
extends
Exception {
public
NotBigEnoughException() {
super
();
}
public
NotBigEnoughException(String msg) {
super
(msg);
}
}
|
package
com.zj.exception;
import
com.zj.exception.types.NotBigEnoughException;
public
class
Recover {
protected
int
current
= 0;
protected
boolean
accept
=
false
;
public
Recover() {}
public
Recover(
int
cur) {
current
= cur;
}
public
void
increment() {
++
current
;
}
public
boolean
passed() {
return
accept
;
}
public
void
passing()
throws
NotBigEnoughException {
if
(
current
> 2) {
accept
=
true
;
System.
out
.println(
"accept "
+
current
);
}
else
throw
new
NotBigEnoughException(
"reject "
+
current
);
}
public
static
void
main(String[] args) {
Recover re =
new
Recover();
while
(!re.passed()) {
try
{
re.passing();
}
catch
(NotBigEnoughException e) {
System.
out
.println(e);
re.increment();
}
}
}
}
|
package
com.zj.exception.types;
public
class
UnknowException
extends
Exception {
public
UnknowException() {
super
();
}
public
UnknowException(String msg) {
super
(msg);
}
}
|
//couldn't throws new exceptions where not found in its base class
public
void
passing()
throws
NotBigEnoughException, UnknowException {
if
(
current
> 2) {
accept
=
true
;
System.
out
.println(
"accept "
+
current
);
}
else
if
(
current
>= 0)
throw
new
NotBigEnoughException(
"reject "
+
current
);
else
throw
new
UnknowException(
"i don't know how to deal with "
+
current
);
}
|
//sure passing(),so not have to throw exceptions
public
void
passing(){
while
(!passed()) {
try
{
super
.passing();
}
catch
(NotBigEnoughException e) {
increment();
}
}
}
|
public
void
fortifiedPassing()
throws
NotBigEnoughException, UnknowException{
try
{
super
.passing();
}
catch
(NotBigEnoughException e) {
if
(
current
>=0)
throw
e;
else
throw
new
UnknowException(
"i don't know how to deal with "
+
current
);
}
}
|
package
com.zj.exception;
import
com.zj.exception.types.NotBigEnoughException;
import
com.zj.exception.types.UnknowException;
public
class
Inheritor
extends
Recover {
public
Inheritor(
int
cur) {
super
(cur);
}
//couldn't throws new exceptions where not found in its base class
/**
public
void
passing()
throws
NotBigEnoughException,
UnknowException
{
if
(current
>
2)
{
accept
=
true;
System.out.println("accept
"
+
current);
}
else
if
(current
>=
0)
throw
new
NotBigEnoughException("reject
"
+
current);
else
throw
new
UnknowException("i
don't
know
how
to
deal
with
"
+
current);
}*/
//sure passing(),so not have to throw exceptions
public
void
passing(){
while
(!passed()) {
try
{
super
.passing();
}
catch
(NotBigEnoughException e) {
increment();
}
}
}
public
void
fortifiedPassing()
throws
NotBigEnoughException, UnknowException{
try
{
super
.passing();
}
catch
(NotBigEnoughException e) {
if
(
current
>=0)
throw
e;
else
throw
new
UnknowException(
"i don't know how to deal with "
+
current
);
}
}
public
static
void
main(String[] args) {
// not required try-catch
new
Inheritor(3).passing();
new
Inheritor(1).passing();
new
Inheritor(-1).passing();
//no exceptions
try
{
new
Inheritor(3).fortifiedPassing();
}
catch
(NotBigEnoughException e) {
e.printStackTrace();
}
catch
(UnknowException e) {
System.
out
.println(e);
}
//NotBigEnoughException:
try
{
new
Inheritor(1).fortifiedPassing();
}
catch
(NotBigEnoughException e) {
e.printStackTrace();
}
catch
(UnknowException e) {
System.
out
.println(e);
}
//UnknownException:
try
{
new
Inheritor(-1).fortifiedPassing();
}
catch
(NotBigEnoughException e) {
e.printStackTrace();
}
catch
(UnknowException e) {
System.
out
.println(e);
}
}
}
|
package
com.zj.exception;
import
com.zj.exception.types.NotBigEnoughException;
import
com.zj.exception.types.UnknowException;
public
class
Wrapper
extends
Inheritor {
public
Wrapper(
int
cur) {
super
(cur);
}
public
void
fortifiedPassing() {
try
{
super
.fortifiedPassing();
}
catch
(NotBigEnoughException e) {
throw
new
RuntimeException(e);
}
catch
(UnknowException e) {
throw
new
RuntimeException(e);
}
}
public
static
void
main(String[] args) {
// not required try-catch
new
Wrapper(3).fortifiedPassing();
new
Wrapper(1).fortifiedPassing();
new
Wrapper(-1).fortifiedPassing();
}
}
|