资源预览内容
第1页 / 共46页
第2页 / 共46页
第3页 / 共46页
第4页 / 共46页
第5页 / 共46页
第6页 / 共46页
第7页 / 共46页
第8页 / 共46页
第9页 / 共46页
第10页 / 共46页
亲,该文档总共46页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 / impossible 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 disrupts 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 input: 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 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 from 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 handled 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 standard 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 will 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.out.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 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 ( : ), 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 were 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 conditions 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.javaCheckExceptionDemo.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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号