Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 56 Runtime Errors

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 56 Runtime Errors"— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 56 Runtime Errors
Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

2 Syllabus Anticipating Error Sources errno Checking Assertion Checking

3 Anticipating Error Sources
Look for “risky” code sections that could adversely affect the program. processing user input performing calculations that may underflow / overflow or use out-of-domain numbers accessing disk files performing system I/O communicating over a network manipulating pointers overstepping array boundaries 2

4 The algorithm designer should:
Perform a "desk check" of the algorithm to manually verify the correctness of each step. Analyze the effects of boundary values and pathological conditions. The programmer should: Test all data and setup conditions for correctness before a section of code is executed that could possibly cause an error. Test any results for correctness after the code section has finished executing. 3

5 errno Checking C provides a way to return an error status value after executing certain library functions. To use this capability, add the directive #include <errno.h> to the source code. This header file defines the errno variable and various error number macros. 4

6 Perform these steps: Set errno to zero before executing a library function that can change errno. After the library function is executed, test the value of errno. If it is still zero, then no error occurred. Otherwise, errno will contain an integer number that corresponds to a particular predefined error condition (see contents of errno.h for details). 5

7 errno checking has some disadvantages:
The strerror() function prints an error message corresponding to the error number. (Add #include <string.h>) errno checking has some disadvantages: Not all library functions return their error status through the errno variable. errno cannot warn of potential errors; it reports after an error has already occurred. errno reports only a predefined set of errors. Other problems could still exist! 6

8 Assertion Checking C supports assertions, which are conditions that must be true at a given point in a program. Before assertion checking can be used, an #include <assert.h> directive is needed. The assert macro is embedded in the program at locations where an assertion needs checking. 7

9 Syntax: assert(condition);
where condition is any valid expression that evaluates to true (non-zero) or false (zero). Example: assert(x >= 0); When the assert macro is executed, condition is evaluated and tested. If true, execution continues at the next statement. If false, the program terminates and a system error message is printed. 8

10 The NDEBUG macro (i.e., No DEBUG) can disable assertion checking.
Assertions are useful for validating correctness during the software development stage. Due to run-time overhead, assertions are often disabled once the program is considered complete and ready for distribution. The NDEBUG macro (i.e., No DEBUG) can disable assertion checking. If used, the macro should be defined before the #include <assert.h> directive. 9


Download ppt "ECE 103 Engineering Programming Chapter 56 Runtime Errors"

Similar presentations


Ads by Google