JAVA课件SSD3 Lecture 3.ppt


文档分类:IT计算机 | 页数:约46页 举报非法文档有奖
1/46
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/46
文档列表 文档介绍
JAVA课件SSD3___Lecture_3Object-Oriented Programming (Java)
Topics Covered Today
Unit Java Applications
 Exception Objects
Code Convention
 Javadoc
2
Program Errors
Syntax (compiler) errors
Errors in code construction (grammar, types)
Detected pilation
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 code
3
Exceptions
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's 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 file
4
Method 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
}
5
The 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;
} } } }
6
Exception Handling
Exception handling is a mechanism that allows failures to be handled outside the normal flow of the code.
将程序运行中的所有错误都看成一种异常,通过对语句块的检测,一个程序中的所有异常被集中起来放到程序中的某一段中进行处理
int readInteger (

JAVA课件SSD3 Lecture 3 来自淘豆网m.daumloan.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数46
  • 收藏数0 收藏
  • 顶次数0
  • 上传人mh900965
  • 文件大小704 KB
  • 时间2017-11-17