java课件ssd3___lecture_3

上传人:第*** 文档编号:48811468 上传时间:2018-07-20 格式:PPT 页数:46 大小:704KB
返回 下载 相关 举报
java课件ssd3___lecture_3_第1页
第1页 / 共46页
java课件ssd3___lecture_3_第2页
第2页 / 共46页
java课件ssd3___lecture_3_第3页
第3页 / 共46页
java课件ssd3___lecture_3_第4页
第4页 / 共46页
java课件ssd3___lecture_3_第5页
第5页 / 共46页
点击查看更多>>
资源描述

《java课件ssd3___lecture_3》由会员分享,可在线阅读,更多相关《java课件ssd3___lecture_3(46页珍藏版)》请在金锄头文库上搜索。

1、Object-Oriented Programming (Java)Topics Covered Today Unit 1.1 Java Applications 1.1.5 Exception Objects 1.1.6 Code Convention 1.1.7 Javadoc2Program Errors Syntax (compiler) errors Errors in code construction (grammar, types) Detected during compilation Run-time errors Operations illegal / impossib

2、le to execute Detected during program execution Treated as exceptions in Java Logic errors Operations leading to incorrect program state May (or may not) lead to run-time errors Detect by debugging code3Exceptions An exception is an event, which occurs during the execution of a program, that disrupt

3、s the normal flow of the programs instructions. Examples Division by zero Access past end of array Out of memory Number input in wrong format (float vs. integer) Unable to write output to file Missing input file4Method readInteger() Pseudo-code for a method that reads an integer from the standard in

4、put: This pseudo-code ignores the failures that may occur: The string cannot be read from the standard input. For example, the standard input may be a damaged file. The string does not contain an integer. For example, the user may type “2r“ instead of “25“. int readInteger () Read a string from the

5、standard input Convert the string to an integer value Return the integer value 5The Traditional Approach Include conditional statements to detect and handle program failures. The code is difficult to read and maintain. int readInteger () while (true) /read a string from the standard input;if (read f

6、rom the standard input fails) handle standard input error; else /convert the string to an integer value; if (the string does not contain an integer) handle invalid number format error; else return the integer value; 6Exception Handling Exception handling is a mechanism that allows failures to be han

7、dled outside the normal flow of the code. 将程序运行中的所有错误都看成一种异常,通过对语 句块的检测,一个程序中的所有异常被集中起来放到 程序中的某一段中进行处理 int readInteger () while (true) try read a string from the standard input; convert the string to an integer value; return the integer value; catch (read from the standard input failed) handle stand

8、ard input error; catch (the string does not contain an integer) handle invalid number format error; normal flow of the code each failure is handled in a catch block 7Run-time Exception public class DivideByZero public static void main(String args) System.out.println(5/0);System.out.println(“this wil

9、l not be printed.“);System.out.println(“Division by zero.“); Exception in thread “main“ java.lang.ArithmeticException: / by zero at DivideByZero.main(DivideByZero.java:9)8try-catch public class DivideByZero1 public static void main(String args) trySystem.out.println(5/0); catch(Exception e)System.ou

10、t.printf(“Caught runtime exception = %s“, e); Caught runtime exception = java.lang.ArithmeticException: / by zero9Exception Object In Java, an exception is an object that describes an abnormal situation. An exception object contains the following information: The kind of exception A call stack which

11、 indicates where the exception occurred A string with additional information about the exception 10Method of Exception String getMessage(). Obtains the argument that was passed to the constructor of the exception object. String toString(). The name of the exception (its type), followed by a colon (

12、: ), followed by the String returned by method getMessage. void printStackTrace(). This method displays, in the standard error stream, the String returned by method toString, followed by information that shows where the exception was thrown. The information includes a list of all the methods that we

13、re called before the exception occurred. 11Exception Class Hierarchy12Exception Class Hierarchy Class Error is used for serious problems from which an application is unlikely to recover. An example is the “out of memory“ error. 13Exception Class Hierarchy Class Exception is used for abnormal conditi

14、ons that an application can be expected to handled. 14Checked public class CheckedExceptionDemo public static void main(String args) FileInputStream stream = new FileInputStream(args0);/ Process file / .stream.close();18Code Compile ErrorsF:My Teachingssd3f07javac CheckExceptionDemo.javaCheckExcepti

15、onDemo.java:4: 未报告的异常 java.io.FileNotFoundException;必 须对其进行捕捉或声明以便抛出FileInputStream stream = new FileInputStream(args0);CheckExceptionDemo.java:7: 未报告的异常 java.io.IOException;必须对其进 行捕捉或声明以便抛出stream.close();2 错误19Catching Multiple Exceptionsimport java.io.*;public class CheckExceptionDemo public static void main(String args) try FileInputStream stream = new FileInputStream(args0);stream.close(); catch (ArrayIndexOutOfBoundsException aiobe) System.err.println(“The program needs a command argument.“); catch (FileNotFoundException exception) System.err.println(“Cannot find file

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 外语文库 > 英语学习

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号