课件java程序计方案.ppt

  1. 1、本文档共48页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
课件java程序计方案

Handling Exceptions(处理异常) Throwing an exception(抛出异常) When an error occurs within a method. An exception object is created and handed off to the runtime system(运行时系统). The runtime system must find the code to handle the error. Catching an exception(捕获异常) The runtime system(运行时系统) searches for code to handle the thrown exception. It can be in the same method or in some method in the call(调用) stack(堆栈). chapter 8 Exceptions */47 Keywords for Java Exceptions try Marks the start of a block associated with a set of exception handlers. catch If the block enclosed by the try generates an exception of this type, control moves here; watch out for implicit subsumption. finally Always called when the try block concludes, and after any necessary catch handler is complete. throws Describes the exceptions which can be raised by a method. throw Raises an exception to the first available handler in the call stack, unwinding the stack along the way. */47 抛出异常– throw, throws 在一个方法的运行过程中,如果一个语句引起了错误时,含有这个语句的方法就会创建一个包含有关异常信息的异常对象,并将它传递给Java运行时系统。 我们把生成异常对象并把它提交给运行时系统的过程称为抛出(throw)异常。 throw — 在方法体中用throw手工抛出异常; throws — 在方法头部间接抛出异常,即:申明方法中可能抛出的异常。 */47 1. 在方法体中用throw手工抛出异常 throw抛出异常,可以是系统定义的异常,也可以是用户自定义的异常。 语法格式: 或 例如:下面语句就抛出了一个IOException异常: throw new IOException(); 异常类名 对象名 =new 异常类构造函数; throw 对象名; throw new 异常类构造函数; */47 throw -Example class ThrowStatement extends Exception { public static void exp(int ptr) { try{ if (ptr == 0) throw new NullPointerException(); } catch(NullPointerException e) { } } public static void main(String[] args) { int i = 0; ThrowStatement.exp(i); } } */47 2. throws—间接抛出异常(申明异常) 一个方法不处理它产生的异常,而是沿着调用层次向上传递,由调用它的方法来处理这些异常,叫声明异常。 在定义方法时用throws关键字将方法中可能产生的异常间接抛出。 若一个方法可能引发一个异常,但它自己却没有处理,则应该声明异常,并让其调用者来处理这个异常,这时就需要用throws关键字来指明方法中可能引发的所有异常。 类型 方法名(参数列表) throws 异常列表 { //…代码 } */47 Example 1: public class ExceptionTest{ void Proc(int sel) thro

文档评论(0)

181****9125 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档