7 异常处理
7.0 本章内容 1 异常处理概述 2 异常处理机制 3 用户自定义异常
7.1 异常 (Exception)处理概述 传统的异常处理方法:程序运行过程中的错误导致异常,传统的面向过程的程序通过条件判断语句进行错误处理。 Java的异常处理方法:Java引入异常处理概念,在一个方法运行过程中,如果发生了异常,则该方法产生一个异常对象,由系统寻找相应代码处理这一异常。 阅读文档: 思考:如果找不到处理代码,怎么办? 异常的概念 由于硬件设备、软件设计导致程序的运行过程中出现的错误称之为。 异常概念实例 HelloArgs.java
7.1 异常 (Exception)处理概述 程序错误分为语法错误和运行错误,语法错误在编译时由编译器检查。 运行错误包括链接错误,内存溢出,文件读写失败,网络中断,强转失败,数组下标越界等等。 Java提供异常处理机制将异常处理程序和主体程序分离出来,并将各种异常信息进行分类处理。 异常对象可以由虚拟机产生也可以由程序生成。 如何理解? 一切异常类都是从Throwable类继承过来的,该类定义在java.lang语言包中。 异常类的继承结构见下页。
January 15, 1991 "Stealth Project" (as named by Scott McNealy) brainstorming meeting in Aspen with Bill Joy, Andy Bechtolsheim, Wayne Rosing, Mike Sheridan, James Gosling and Patrick Naughton. February 1, 1991 Gosling, Sheridan, and Naughton begin work in earnest. Naughton focuses on "Aspen" graphics system, Gosling on programming language ideas, Sheridan on business development. June 1991 Gosling starts working on the "Oak" interpreter, which, several years later (following a trademark search), is renamed "Java." August 19, 1991 Green team demonstrates basic user interface ideas and graphics system to Sun co-founders Scott McNealy and Bill Joy. Summer 1992 Massive amounts of hacking on Oak, and related components. October 1, 1992 Wayne Rosing joins from SunLabs (which had formed in July 1990) and assumes management of the team. March 15, 1993 The development team, now incorporated as FirstPerson, focuses on interactive television after learning about Time Warner's RFP for its interactive cable TV trial in Orlando, FL. April, 1993 NCSA Mosaic 1.0, the first graphical browser for the Internet, is released. June 14, 1993 Time Warner goes with SGI for its interactive cable TV trial, despite acknowledged superiority of Sun technology and assurances in mid-April that Sun won the deal. Summer, 1993 Naughton flies 300,000 miles selling Oak to anyone involved in consumer electronics and interactive television; meanwhile, the rate at which people are gaining access to the Internet reaches breakneck speed. August, 1993 After months of promising negotiations with 3DO to provide set-top box OS, 3DO president Trip Hawkins offers to buy the technology outright. McNealy refuses, and deal falls through. September, 1993 Arthur Van Hoff joins team, originally to do application development environment aimed at interactive television; ends up doing mostly language design. February 17, 1994 Alternative FirstPerson business plan for doing CD-ROM/online multimedia platform based on Oak presented to Sun executives to very mixed reviews. April 25, 1994 Sun Interactive created, half of FirstPerson employees leave to join it. June, 1994 "Liveoak" project started. Designed by Bill Joy to use Oak for a big small operating system project. July, 1994 Naughton reduces the "Liveoak" project's scope to simply retargeting Oak at the Internet after writing a throwaway implementation of a Web browser in a long weekend hack. September 16, 1994 Jonathon Payne and Naughton start writing "WebRunner," a Mosaic-like browser later renamed "HotJava" September 29, 1994 HotJava prototype is first demonstrated to Sun executives. Autumn, 1994 Van Hoff implements Java compiler in Java. (Gosling had previously implemented it in C.) May 23, 1995 Sun formally announces Java and HotJava at SunWorld '95. Netscape announces its intention to license Java for use in Netscape browser. September 21, 1995 Sun-sponsored Java development conference held in New York City. September 25, 1995 Sun announces expanded alliance with Toshiba and a joint project to develop remote information retrieval products which incorporate Java. September 26, 1995 Sunsoft announces suite of business-oriented development products incorporating Java. October 30, 1995 Oracle announces its WebSystem suite of WWW software which includes a Java-compatible browser. At the Internet World Conference in Boston, Lotus Development Corp., Intuit Inc., Borland International Inc., Macromedia Inc.,and Spyglass Inc. announce plans to license Java. December 4, 1995 Sun and Netscape announce Javascript, a scripting language based on the Java language which is designed to be accessible to non-programmers. Sun, Netscape and Silicon Graphics announce new software alliance to develop Internet interactivity tools. Borland, Mitsubishi Electronics, Sybase and Symatec annouce plans to license Java. December 6, 1995 IBM and Adobe announce licensing agreement with Sun for use of Java. December 7, 1995 Microsoft announces plans to license Java during announcement of suite of new Internet products, including Visual Basic Script.
7.1 异常 (Exception)处理概述 运行异常是指通过提高编程完全可以避免的异常,例如: ArrayIndexOutOfBoundsException, ClassCastException 这类异常编译器不予理会,完全交给程序员处理。 检查异常是指无论如何改进程序都无法避免的异常,例如:FileNotFoundException, IOException, SQLException 这类异常在程序里必须显式地处理。 Error类错误在程序中一般也不处理,因为它们的严重程度足以使VM无法继续运行。
7.2异常处理机制(Exception) Java处理异常步骤。 (1)Java程序执行过程中如果出现异常,会自动生成一个异常对象,该异常 对象被提交Java运行时系统,这个过程称为抛出异常。抛出异常也可以由 用户程序自定义。 (2)当Java运行时系统接收到异常对象时,会寻找处理这一异常的代码并把 当前异常对象交给其处理,这一过程叫捕获异常。 (3)如果Java运行时系统找不到可以处理异常的方法,则运行时系统终止, 相应的Java 应用程序退出。
7.2 异常处理机制(Exception) 捕获异常 try…catch…finally 声明异常 一个方法如果本身不处理异常,该方法可以声明抛出异常,使得异常对象可以从调用栈向后传播,直到有合适的方法捕获它为止。声明抛出异常由方法声明中的throws子句指明,语法格式如下: type methodName ([paramList]) [throws exceptionList]{ ……} 抛出异常, throw语句的语法格式是: throw <异常对象> 示例:ReadFile. java TestThrowsException.java
7.2异常处理机制(Exception)
7.2异常处理机制(Exception) 一般应用中定义的异常都是检查异常。 建模初期没有必要细化异常类,简单地定义一个应用异常就可以了,以后在从该类继承下来进行细化。(A) finally语句块一定会执行,除非VM错误或显式地调用System.exit(0)语句。 throws声明中,任何异常定义都可以。(B) 异常对象携带的文本描述信息可以通过构造器参数传递进去(见异常类构造器定义),该信息可以通过getMessage()方法获得。 异常对象广播时,方法栈中的语句执行信息可以通过printStackTrace()打印输出。
7.2异常处理机制(Exception) 重复异常处理机制: (1)Java程序的执行过程中如出现异常,会自动生成一个异常类对象,该异常对象将被提交给Java运行时系统,这个过程称为抛出异常。抛出异常也可以由程序来强制进行。 (2)当Java在运行时系统接收到异常对象,会寻找能处理这一异常的代码并把当前异常对象交给其处理,这一过程称为捕获(catch)异常。 (3)如果Java运行时系统找不到可以捕获异常的方法,则运行时系统将终止,相应的Java程序也将退出。
异常的处理过程示例
7.3用户自定义异常
实例: //业务逻辑描述,用户登记,当号码为负值时,抛出用户自定义异常。 UserDefinedException.java
总结 一个图 五个关键字 try catch finally throws throw 先捕捉特殊的异常,再一般的异常 异常和重写的关系 自定义异常