ECE 103 Engineering Programming Chapter 56 Runtime Errors

Slides:



Advertisements
Similar presentations
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Advertisements

Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Programming Types of Testing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
ECE 103 Engineering Programming Chapter 7 Compiling C Programs Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
ECE 103 Engineering Programming Chapter 15 C Standard Library Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
13 C Preprocessor.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Chapter 4: Control Structures I (Selection)
Introduction to Computing Science and Programming I
Chapter 6: User-Defined Functions I
Chapter 6 CS 3370 – C++ Functions.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
UT-Assert Library Presented by Charles Zogby, NASA-GSFC
Chapter 13 - The Preprocessor
CSE 374 Programming Concepts & Tools
Chapter 2 Assignment and Interactive Input
The Selection Structure
CS 106 Computing Fundamentals II Chapter 77 “Algorithm”
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
User-Defined Functions
Chapter 2 part #3 C++ Input / Output
Outline Altering flow of control Boolean expressions
CS 161 Introduction to Programming
Programming Funamental slides
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Design and Implementation
Herbert G. Mayer, PSU CS Status 8/2/2013
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
ECE 103 Engineering Programming Chapter 62 Stack Implementation
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
CHAPTER 4: Conditional Structures
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
Herbert G. Mayer, PSU CS Status 7/19/2015
Chapter 2 part #3 C++ Input / Output
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
ECE 103 Engineering Programming Chapter 18 Iteration
ECE 103 Engineering Programming Chapter 22 Selection
CSE 1020:Software Development
CS-1020 and Exception Handling
Chapter 1 c++ structure C++ Input / Output
CHAPTER 6 Testing and Debugging.
Presentation transcript:

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 Wong @ PSU ECE

Syllabus Anticipating Error Sources errno Checking Assertion Checking

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

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

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

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

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

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

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

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