DOC PREVIEW
UMD CMSC 132 - Program Correctness, Exceptions

This preview shows page 1-2-3-18-19-37-38-39 out of 39 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 132 Object Oriented Programming II Program Correctness Exceptions Department of Computer Science University of Maryland College Park Overview Program correctness is determined by the presence absence of program defects errors Issues Types of errors Testing Debugging Exceptions Program Errors Types of errors Compile time syntax errors Run time errors Logic errors 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 Program Errors Logic Logic errors Operations leading to incorrect program state May or may not lead to run time errors Problem in design or implementation of algorithm Examples Computing incorrect arithmetic value Ignoring illegal input Hardest error to handle Detect by testing Fix by debugging Testing Run program or part of program under controlled 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 Test coverage Test Coverage Test coverage Whether code is executed by some test case Automatically calculated by submit server For set of tests selected from link E g student tests public tests student public tests For conditionals reports X Y where X tests executing True Y tests executing False Color Green executed by some test case Pink not executed Test Coverage Example Debugging Process of finding and fixing software errors After testing detects error Goal Determine cause of run time logic errors Correct errors without introducing new errors Similar to detective work Carefully inspect information in program Code Values of variables Program behavior 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 Single step Execute single line of code at a time When executing method can Finish entire method Execute first line in method Tedious or impractical for long running programs Breakpoint Specify location s in code Execute program until breakpoint encountered Can skip past uninteresting code Single Step Eclipse Debugger Breakpoint Data Display 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 Performing action in response to exception Example actions Ignore exception Print error message Request new data Retry action Approaches 1 Exit program 2 Exit method returning error code 3 Throw exception Problem May not be able to handle error locally Not enough information in method class Need more information to decide action Handle exception in calling function s instead Decide at application level instead of library Examples Incorrect data format ask user to reenter data Unable to open file ask user for new filename Insufficient disk space ask user to delete files Will need to propagate exception to caller s Exception Handling Exit Program Approach Exit program with error message error code Example if error System err println Error found System exit 1 message error code Problem Drastic solution Event must be handled by user invoking program Program may be able to deal with some exceptions Exception Handling Error Code Approach Exit function with return value error code Example A if error return 1 B if retval A 1 return 1 Problems Calling function must check process error code May forget to handle error code May need to return error code to caller Agreement needed on meaning of error code Error handling code mixed with normal code Exception Handling Throw Exception Approach Throw exception Example A if error throw new ExceptionType Java exception backtracks to B caller s until matching catch try block found A catch ExceptionType e action Exception Handling Throw Exception Advantages Compiler ensures exceptions are caught eventually No need to explicitly propagate exception to caller Backtrack to caller s automatically Class hierarchy defines meaning of exceptions No need for separate definition of error codes Exception handling code separate clearly marked Representing Exceptions in Java Exceptions represented as Objects derived from class Throwable Code public class Throwable extends Object Throwable No error message Throwable String mesg Error message String getMessage Return error mesg void printStackTrace Record methods called location Representing Exceptions Java Exception class hierarchy Two types of exceptions checked unchecked Representing Exceptions Java Exception class hierarchy ClassNotFoundException Exception CloneNotSupportedException IOException ArithmeticException AWTException NullPointerException RuntimeException Object IndexOutOfBoundsException Throwable NoSuchElementException LinkageError VirtualMachoneError Error AWTError Checked Unchecked Unchecked Exceptions Class Error RunTimeException Serious errors not handled by typical program Usually indicate logic errors Example NullPointerException IndexOutOfBoundsException Catching unchecked exceptions is optional Handled by Java Virtual Machine if not caught Checked Exceptions Class Exception except RunTimeException Errors typical program should handle Used for operations prone to error Example IOException ClassNotFoundException Compiler requires catch or declare Catch and handle exception in method OR Declare method can throw exception force calling function to catch or declare exception in turn Example void A throws ExceptionType Generating Handling Exceptions Java primitives Try Throw Catch Finally Procedure for using exceptions 1 Enclose code generating exceptions in try


View Full Document

UMD CMSC 132 - Program Correctness, Exceptions

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Loading Unlocking...
Login

Join to view Program Correctness, Exceptions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Program Correctness, Exceptions and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?