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.5 运行时错误

如果你的程序没有编译错误和链接错误的话,那么它就能运行了。现在才是乐趣的真正开始。在你写程序的时候,你能够轻易地发现错误。但是,对于运行程序时发现的错误,你可能很难确定如何解决它,例如:

在程序中,我们使用变量x,y,z(而不是直接使用数值作为参数),这使得阅读代码的人和编译器更难发现问题。但是,这些调用会把负数赋给area1和area2,导致面积表示为负数。我们能够接受这种明显违背数学和物理规律的错误结果么?如果不能,应该由谁来检测这种错误:area()的调用者或者函数本身?这种错误又应该如何被报告呢?

在回答这些问题之前,我们先看看上面代码中ratio的计算。这条语句看上去没有什么问题。但你是否发现了什么不对的地方?如果没有,再仔细看看:area3的值是0,因此double(area1)/area3将除以0。这会导致一个硬件检测错误并终止程序,同时报出一个与硬件相关的错误信息。这类错误就是运行时错误。如果你或者你的用户没有及时发现并解决这类错误,在程序运行时这类错误就可能出现。对于这类“硬件错误”,大多数人的容忍度都很低。因为他们不了解程序的细节而仅仅知道“某个地方出了问题”。这点信息对于处理问题帮助很小。在这种情况下,人们会很生气并对程序的提供者抱怨连连。 ...

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