import java.util.*;class BaseballException extends Exception {}
class Foul extends BaseballException {}
class Strike extends BaseballException {}abstract class Inning
{
public Inning() throws BaseballException {}
public void event() throws BaseballException {}
public abstract void atBat() throws Strike, Foul;
public void walk() {}
}class StormException extends Exception {}
class RainedOut extends StormException {}
class PopFoul extends Foul {}interface Storm
{
public void event() throws RainedOut;
public void rainHard() throws RainedOut;
}
public class MyException extends Inning implements Storm
{
public MyException() throws RainedOut, BaseballException {}
public MyException(String s) throws Foul, BaseballException {}
public void rainHard() throws RainedOut {}
public void event() {}
public void atBat() throws PopFoul {}
public static void main(String[] args)
{
try
{
MyException si = new MyException();
si.atBat(); //A
}
catch(PopFoul e) //1
{
System.err.println("Pop foul");
}
catch(RainedOut e) //2
{
System.err.println("Rained out");
}
catch(BaseballException e) //3
{
System.err.println("Generic baseball exception");
}
try
{
Inning i = new MyException();
i.atBat(); //B
}
catch(Strike e) //4
{
System.err.println("Strike");
}
catch(Foul e) //5
{
System.err.println("Foul");
}
catch(RainedOut e) //6
{
System.err.println("Rained out");
}
catch(BaseballException e) //7
{
System.err.println("Generic baseball exception");
}
}
}书上是这样解释的:
如果你用的是具体的MyException对象的references的话,编译哭只会强制要求你捕获这个类抛出的异常.但是如果你将它上传到基类,那么编译器就会(很严格地)要求你捕获基类的异常.所有这些是为了产生更为强壮的代码.
那么A会什么要捕获1,2,3的异常,而B要捕获4,5,6,7呢,大家能不能帮我解答一下啊?
A为什么不用捕获Foul的异常啊?
小弟谢谢先...
class Foul extends BaseballException {}
class Strike extends BaseballException {}abstract class Inning
{
public Inning() throws BaseballException {}
public void event() throws BaseballException {}
public abstract void atBat() throws Strike, Foul;
public void walk() {}
}class StormException extends Exception {}
class RainedOut extends StormException {}
class PopFoul extends Foul {}interface Storm
{
public void event() throws RainedOut;
public void rainHard() throws RainedOut;
}
public class MyException extends Inning implements Storm
{
public MyException() throws RainedOut, BaseballException {}
public MyException(String s) throws Foul, BaseballException {}
public void rainHard() throws RainedOut {}
public void event() {}
public void atBat() throws PopFoul {}
public static void main(String[] args)
{
try
{
MyException si = new MyException();
si.atBat(); //A
}
catch(PopFoul e) //1
{
System.err.println("Pop foul");
}
catch(RainedOut e) //2
{
System.err.println("Rained out");
}
catch(BaseballException e) //3
{
System.err.println("Generic baseball exception");
}
try
{
Inning i = new MyException();
i.atBat(); //B
}
catch(Strike e) //4
{
System.err.println("Strike");
}
catch(Foul e) //5
{
System.err.println("Foul");
}
catch(RainedOut e) //6
{
System.err.println("Rained out");
}
catch(BaseballException e) //7
{
System.err.println("Generic baseball exception");
}
}
}书上是这样解释的:
如果你用的是具体的MyException对象的references的话,编译哭只会强制要求你捕获这个类抛出的异常.但是如果你将它上传到基类,那么编译器就会(很严格地)要求你捕获基类的异常.所有这些是为了产生更为强壮的代码.
那么A会什么要捕获1,2,3的异常,而B要捕获4,5,6,7呢,大家能不能帮我解答一下啊?
A为什么不用捕获Foul的异常啊?
小弟谢谢先...
解决方案 »
- Permgen OutOfMemory,请问有什么手段或者工具可以看到Permgen中被intern()的String的具体内容
- 就是我想点“下”,光标就跑到con里面,点“上”就跑到pro中
- 做出来500分
- 关于按钮事件的问题。
- JavaIDE工具,JawaBeginner的新版本. 比JCreator好多了.
- 怎样向建好关系的表中存纪录?
- servlet 同 applet 通讯问题
- 关于prepareStatement select like ? 的问题?
- java 调用 C 函数
- mybatis 返回结果map中columnPrefix不生效
- 请教多线程实现图片互换动画
- 一个类中如何编写方法名相同,返回值不同的方法?
try{
.....
}
catch(Exception e)
{}
了,呵呵,我手懒的时候就这样写,谁要是不喜欢,就当是谬论好了。
注意,这里捕获的是Inning产生的异常,而不是其具体的子类。
catch(SQLException e)
{//处理内容}
如果不想关注异常的话就
catch(Exception e)
{}
也就是说如果某一层(A)产生异常不去捕获而直接 throws ,那么上一层(B)在调用此函数时,必须捕获此异常,或再次 throws .