多层的异常捕获

CatchWho.Java

源代码:

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");

}

}

}

预测程序运行结果:ArrayIndexOutOfBoundsException/外层try-catch

发生ArithmeticException

ArrayIndexOutOfBoundsException/外层try-catch

实际运行结果截图:

源代码:

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");

}

}

}

预测程序运行结果:ArrayIndexOutOfBoundsException/内层try-catch

发生ArithmeticException

ArrayIndexOutOfBoundsException/外层try-catch

实际运行结果截图:

时间: 2024-08-25 21:53:12

多层的异常捕获的相关文章

Java多层的异常捕获

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

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

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

用c实现跨平台异常捕获机制

TBOX封装了一套跨平台的异常捕获实现,来模拟windows的seh异常处理功能,而且是线程安全的. 在linux/mac下的实现 使用signal 捕获异常信号 使用sigsetjmp保存现场寄存器和信号掩码,出现异常后使用 siglongjmp 跳转到异常处理过程,并恢复状态 使用线程局部存储维护 sigjmpbuf 寄存器现场状态堆栈,保证多线程安全,并且可以实现多层嵌套捕获处理. 在windows下的实现 这个就不用多说了,在vs下直接用 try.except 关键字就行了,如果在min

.NET 基础 一步步 一幕幕[数组、集合、异常捕获]

数组.集合.异常捕获 数组: 一次性存储多个相同类型的变量. 一维数组: 语法: 数组类型[] 数组名=new 数组类型[数组长度]; 声明数组的语法: A.数据类型 [] 数组名称= new 数据类型[2]{1,2}: B.数据类型 [] 数组名称 = new 数据类型[数组大小]; C. 数据类型 [] 数组名称 = {数据,数据,数据,数据}; ***数组的长度一旦固定了,就不能再被改变了 可以通过索引来访问数组中的元素: 数组名称[索引位置] 案例: 多维数组:多个线性数组的值 二维:i

异常捕获

异常捕获,在现在很多ide工具里都可以用快捷键很方便的添加上,防止用户看到自己看不懂的报错甚至莫名其妙崩溃,导致用户体验不好. 哪怕显示一个友好一些的崩溃提示,也比直接显示error:xxxx xxxxxxxxx要好得多. 当然最终的目的还是要给出对应的解决办法,让代码可以继续运行. 1 <?php 2 header("content-type:text/html; charset=utf-8"); 3 /** 4 * 包裹重量异常 5 */ 6 class HeavyParce

电脑小白学习软件开发-C#的选择语句、异常捕获,进攻程序员

写代码也要读书,爱全栈,更爱生活.每日更新原创IT编程技术及日常实用视频. 我们的目标是:玩得转服务器Web开发,搞得懂移动端,电脑客户端更是不在话下. 不得不说,C#这门语言是小编以为最好的语言.其优美的语法,最具人性化的新特性,以及无敌的开发工具令人陶醉.接触过不少语言,却一直回味写C#的那种状态. 本人认为目前C#是比较适合入门的语言,最为小白,热衷于电脑编程开发的人,可谓是一个大大的福利. 不管如何写过多少中语言教程,在写C#教程时却是如此的富含感情.为了完成我们的全栈梦,作为服务器端,

WebAPI 过滤器拦截处理以及异常捕获

对action进行拦截处理 public class UserAuthorizeAttribute : ActionFilterAttribute { /// <summary> /// CC调用的Token /// </summary> //private static readonly string CCToken = ConfigurationManager.AppSettings["CCToken"].ToString(); /// <summar

iphone 异常捕获处理

iphone 异常捕获处理 1 void UncaughtExceptionHandler(NSException *exception) { 2 NSArray *arr = [exception callStackSymbols]; 3 NSString *reason = [exception reason]; 4 NSString *name = [exception name]; 5 NSString *urlStr = [NSString stringWithFormat:@"mai

javascript异常捕获笔记

异常捕获 1.异常 当JavaScript引擎执行Javascript代码时,发生了错误,导致程序停止运行 2.异常抛出 当异常产生,并且将这个异常生成一个错误信息 3.异常捕获 try{ 发生异常的代码块: }catch(err){ 错误信息处理: } function demo(){ try{ alert(str); }catch(error){ console.log(error);//==>ReferenceError: str is not defined(-) } } demo();