Program Correctness - Object-Oriented Programming II - Notes | CMSC 132, Study notes of Computer Science

Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-u4y-1
koofers-user-u4y-1 🇺🇸

5

(2)

9 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 132:
Object-Oriented Programming II
Program Correctness
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Program Correctness - Object-Oriented Programming II - Notes | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Program Correctness

Department of Computer Science

University of Maryland, College Park

Overview

Program correctness is determined by thepresence / absence of

program defects

(errors)

Issues

Types of errors Exceptions Testing Debugging

Program Errors – Compile Time

Compile-time (syntax) errors

Errors in code construction

Lexical (typographical), grammatical, types Detected during compilation Usually easy to correct quickly

Examples

Misspelled keyword Missing or misplaced symbol Incorrect operator for variable type

Program Errors – Run Time

Run-time errors

Operations illegal / impossible to execute Detected during program execution

But not detectable at compile time Treated as

exceptions

in Java

Example

Division by zero Array index out of bounds Using null pointer Illegal format conversion

Exceptions

Rare event outside normal behavior of code

Usually a

run-time error

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

Exception Handling – Throw Exception

Approach

Throw exception

Example^ A( ) {

if (error)

throw

new ExceptionType();

} B( ) {

try {

A( );

} catch

(ExceptionType e) { ...action... }

Java exception backtracks tocaller(s) until matching catchblock found

Generating & Handling Exceptions^ Java primitives

Try Throw Catch Finally

Procedure for using exceptions 1.

Enclose code generating exceptions in

try

block

Use

throw

to actually generate exception

Use

catch

to specify exception handlers

Use

finally

to specify actions after exception

Java Syntax

try

// try block encloses throws

throw

new eType1();

// throw

jumps

to catch

} catch

(eType1 e) {

// catch block 1

...action...

// run if type match

} catch

(eType2 e) {

// catch block 2

...action...

// run if type match

} finally

// final block

...action...

// always executes

Exceptions – Examples

Used in programming project^ public void MethodRequiredForProject() {

throw new UnsupportedOperationException(

"You must implement this method.");

} Behavior

If method is invoked during program execution Exception is thrown

Of type UnsupportedOperationException Message string is displayed Program execution stops unless exception caught

Testing

Run program (or part of program) undercontrolled conditions to verify behavior

Detects

run-time error

if exception thrown

Detects

logic error

if behavior is incorrect

Issues

Selecting test cases Testing different parts of program Visibility of program code

Covered in more detail later…

Debugging – Approaches

Classic

Insert debugging statements Trace program control flow Display value of variables

Modern

IDE (integrated development environment) Interactive debugger

Interactive Debugger

Capabilities

Provides trace of program execution Shows location in code where error encountered Interactive program execution

Single step

through code

Run to

breakpoints

Displays values of variables

For current state of program

Interactive Debugger

Breakpoint

Specify location(s) in code Execute program until breakpoint encountered Can skip past uninteresting code