动手动脑:多层的异常捕获

示例1

public class CatchWho {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}

throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}

示例2

public class CatchWho2 {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}

时间: 2024-10-11 22:01:45

动手动脑:多层的异常捕获的相关文章

Java多层的异常捕获

示例程序一: public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch

多层的异常捕获

CatchWho.Java 源代码: public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); //要处理的问题 } catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException"+

java09动手动脑

一.动手动脑 运行AboutException.java示例 1)源代码 import javax.swing.*; class AboutException { public static void main(String[] a) { double i=-1, j=0, k; k=i/j; try { k = i/j; // Causes division-by-zero exception //throw new Exception("Hello.Exception!"); }

动手动脑-异常

public class CatchWho{ public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(  "ArrayIndexOutOfBoundsException" +  "内层try-catch")

动手动脑接口与继承

(1)可以使用instanceof运算符判断一个对象是否可以转换为指定的类型: 参看实例: TestInstanceof.java public class TestInstanceof { public static void main(String[] args) { //声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类 //但hello变量的实际类型是String Object hello = "Hello"; //String

java 动手动脑7

---恢复内容开始--- 一.动手动脑:多层的异常捕获-1 阅读以下代码(CatchWho.java),写出程序运行结果: ArrayIndexOutOfBoundsException/内层try-catch 发生ArithmeticException 1.源码: public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException

java动手动脑异常处理

实验任务一:多层的异常捕获-1 1.实验内容:阅读(CatchWho.java),写出程序运行结果: public class CatchWho{ public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(  "ArrayIndexOut

09-异常处理动手动脑及课后实践性问题总结

一.动手动脑:请阅读并运行AboutException.Java示例,然后通过后面的几页ppt了解Java中实现异常处理的基础知识. (1)源代码: import javax.swing.*; class AboutException { public static void main(String[] a) { double i=1, j=0, k; k=i/j; try { k = i/j; // Causes division-by-zero exception //throw new E

第八周动手动脑

动手动脑一: 请阅读并运行AboutException.java示例 AboutException.java 答: 结论: 异常 (Exception):发生于程序执行期间,表明出现了一个非法的运行状况.许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象. 例如:数组越界和被0除 动手动脑二:多层的异常捕获-1 阅读以下代码(CatchWho.java),写出程序运行结果:  CatchWho 答:运行结果: ArrayIndexOutOfBoundsException/内层try-ca