Skip to Content
C++程序设计:原理与实践(基础篇)(原书第2版)
book

C++程序设计:原理与实践(基础篇)(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
9h 36m
Chinese
Pearson
Content preview from C++程序设计:原理与实践(基础篇)(原书第2版)

5.2 错误的来源

错误的来源包括:

·缺少规划:如果没有事先规划好程序要做什么,我们不可能充分检查所有“死角”,并确认所有的可能情况都会被正确处理(即对任意输入程序都能给出正确结果或者充分的错误信息)。

·不完备的程序:在软件开发过程中,显然会有一些情况我们没有考虑到,这是不可避免的。我们必须要达到的目标是掌握我们何时能够处理所有情况。

·意外的参数:函数会使用参数。如果为一个函数输入了一个不能处理的参数的话,我们会遇到问题。例如,为求平方根的标准库函数输入-1.2:sqrt(-1.2)。由于sqrt()的输入和输出都是double,在这种情况下,它不可能输出正确结果。5.5.3节将讨论这种情况。

·意外的输入:典型的程序输入包括键盘、文件、图形界面和网络等。对于这些输入,程序一般都设定了许多前提假设。例如,用户会输入一个数字。但是,如果用户输入的是“喂,闭嘴!”而不是期望的数字,程序会怎样呢?5.6.3节和10.6节将讨论这类问题。

·意外状态:多数程序都保留有很多系统各个部分所使用的数据(“状态”)。例如,地址表、电话簿、温度记录等。如果这些数据是不完整的或者错误的,那应该如何处理呢?无疑程序的各个部分仍然应该正常运转。26.3.5节将讨论这类问题。

·逻辑错误:这表示程序没有按照我们所期望的那样运行。我们不得不查找并修正这些问题。在6.6节和6.9节中我们将给出这类问题的例子。

上述这一列表可以用于实际开发。当我们在开发一个软件的时候,可以把上述列表作为检查表。在我们认为已经排除了所有潜在错误来源之前,软件是不能被交付使用的。实际上,从项目开始时,我们就始终要关注错误处理。因为,没有认真考虑过错误处理的软件几乎是不可能正常工作的,它还会被重新编写一遍。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252