日期:2014-05-20 浏览次数:21052 次
class thro
{
public static void main(String args[])
{
try
{
try
{
throw new ArrayIndexOutOfBoundsException("OOB");
}
catch(ArrayIndexOutOfBoundsException f)
{
System.out.println("捕捉内部的Exception");
throw f;
}
}catch(ArrayIndexOutOfBoundsException f)
{
System.out.println("再捕捉Exception"+f);
}
}
}
//类名,不符合规范,首字母未大写
class thro
{
//类的主方法,正确
public static void main(String args[])
{
try
{
try
{
// 直接抛出一个越界的运行时异常
throw new ArrayIndexOutOfBoundsException("OOB");
}
// 这里捕获越界异常
catch(ArrayIndexOutOfBoundsException f)
{
// 打印异常
System.out.println("捕捉内部的Exception");
// 这里继续抛出异常
throw f;
}
}
// 外层再次捕获异常
catch(ArrayIndexOutOfBoundsException f)
{
// 打印异常
System.out.println("再捕捉Exception" + f);
}
}
}
------解决方案--------------------
class thro {
public static void main(String args[]) {
try {
try {
// 这里手工抛出了异常
throw new ArrayIndexOutOfBoundsException("OOB");
} catch (ArrayIndexOutOfBoundsException f) {
// 这里捕获了,如果是父类类异常,比如Exception 也能捕获
System.out.println("捕捉内部的Exception");
// 这里你又抛出去了
throw f;
}
} catch (ArrayIndexOutOfBoundsException f) {
// 还是同样的异常,应该换成Exception 看看,呵呵
System.out.println("再捕捉Exception" + f);
}
}
}
------解决方案--------------------
这个主要是体现了Exception层层抛出的概念
------解决方案--------------------
mark!
------解决方案--------------------
class ThrowStatDemo {
//class thro { //类名按规范应该是首字符大写的名词或名词词组(其余单词首字符也要大写)
public static void main(String[] args) {
//Java 中提倡将 [] 放数组元素类型后面,能体现出 args 本质上是个对象变量
try {
try {
throw new ArrayIndexOutOfBoundsException("OOB");
//手动创建异常对象并用 throw 语句抛出
} catch (ArrayIndexOutOfBoundsException f) {
//此处 f 的作用域是本 catch 块,不会跟下面的 f 冲突
System.out.println("捕捉内部的Exception");
//捕获到上面抛出的异常并打印出 "捕捉内部的Exception"
throw f;
//再次将捕到的异常上抛 *
}
} catch (ArrayIndexOutOfBoundsException f) {
System.out.println("再捕捉Exception" + f);
//捕获到 * 处抛出的异常并打印出
//"再捕捉Exception" + "java.lang.ArrayIndexOutOfBoundsException: OOB"
//因为异常类 ArrayIndexOutOfBoundsException 的 toString 被设计成返回
//异常类带包名限定的类名加 : 加 new 它时给的异常描述信息 OOB。
}
}
}