3-20100914 - Introduction to OO Program Design Software...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to OO Program Design Software College of SCU Instructor: Shu Li Email: shuli@scu.edu.cn
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Topics Covered Today Unit 1.1 Java Applications 1.1.5 Exception Objects 1.1.6 Code Convention 1.1.7 Javadoc
Background image of page 2
3 Program 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 code
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 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
Background image of page 4
5 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 }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 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;                                } } }  }  
Background image of page 6
7 Exception Handling Exception handling is a mechanism that allows failures to be handled outside the normal flow of the code. f 'sZ ZEZ Z i; v i ; Z 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
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Run-time Exception public class DivideByZero { public static void main(String[] args) { System. out .println(5/0); System. out .println("this will nor be printed."); System. out .println("Division by zero."); } } Exception in thread "main" java.lang.ArithmeticException : / by zero at DivideByZero.main(DivideByZero.java:9 )
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/12/2011 for the course ECON 101 taught by Professor Professor during the Spring '10 term at Cisco Junior College.

Page1 / 46

3-20100914 - Introduction to OO Program Design Software...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online