第四章Java异常的.pptVIP

  • 4
  • 0
  • 约1.36万字
  • 约 41页
  • 2017-10-11 发布于浙江
  • 举报
第四章Java异常的

第 4 章 异常 四川师范大学计算机学院 朱小英 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 第4章 异常 4.1 异常的概念 4.2 异常的分类 4.3 捕获异常 4.4 声明异常 4.5 抛出异常 4.6 创造自己的异常 4.7 总结 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 4.1 异常的概念 什么是异常? 异常实际上是程序中错误导致中断了正常的指令流的一种事件. 没有处理错误的程序: read-file { openTheFile; determine its size; allocate that much memory; closeTheFile; } Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 4.1 异常的概念 以常规方法处理错误 openFiles; if (theFilesOpen) { determine the lenth of the file; if (gotTheFileLength){ allocate that much memory; if (gotEnoughMemory) { read the file into memory; if (readFailed) errorCode=-1; else errorCode=-2; }else errorCode=-4; }else errorCode=-4 ; }else errorCode=-5; Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 4.1 异常的概念 观察前面的程序你会发现大部分精力花在出错处理上了. 只把能够想到的错误考虑到,对以外的情况无法处理 程序可读性差 出错返回信息量太少 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 4.1 异常的概念 用异常的形式处理错误 read-File; { try { openTheFile; determine its size; allocate that much memory; closeTheFile; }catch(fileopenFailed) { dosomething; } catch(sizeDetermineFailed) {dosomething;} catch(memoryAllocateFailed){ dosomething;} catch(readFailed){ dosomething;} catch(fileCloseFailed) { dosomething; } catch(default){dosomething;} } Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 4.1 异常的概念 和传统的方法比较异常的优点: 1.把错误代码从常规代码中分离出来 2. 把错误传播给调 用堆栈 4. 按错误类型和 错误差别分组 4. 系统提供了对于一些无法预测的错误的捕获和处理 5. 克服了传统方法的错误信息有限的问题 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyr

文档评论(0)

1亿VIP精品文档

相关文档