Skip to Content
R 语言经典实例(原书第 2 版)
book

R 语言经典实例(原书第 2 版)

by J.D. Long, Paul Teetor
June 2020
Beginner to intermediate
522 pages
9h 6m
Chinese
China Machine Press
Content preview from R 语言经典实例(原书第 2 版)
简单编程
465
15.7 给出警示错误的信号
15.7.1 问题
当代码遇到严重问题时,需要暂停并提醒用户。
15.7.2 解决方案
调用 stop 函数,它将输出错误消息并终止所有处理。
15.7.3 讨论
当代码遇到致命错误时,停止当前处理至关重要,例如检查账户是否仍有正余额:
if (balance < 0) {
stop("Funds exhausted.")
}
这里调用 stop 函数将显示消息,终止处理,并将返回控制台提示符:
#> Error in eval(expr, envir, enclos): Funds exhausted
出现问题可能有各种原因:数据出错、用户错误、网络故障以及代码中的错误等。原因
可能有很多。适当提前考虑到潜在的问题和代码是很重要的:
检测
至少检测可能的错误。如果无法进一步处理,则停止。未检测到的错误是程序失败
的主要原因。
报告
如果必须停止,请向用户提供合理的解释原因。这将有助于他们诊断和解决问题。
恢复
在某些情况下,代码可能能够自行纠正该情况并继续。但是,我们建议警告用户,
其代码遇到问题并需要纠正。
错误处理是
防御性编程
的一部分,是使代码稳健的做法。
15.7.4 另请参阅
使用 stop 函数的替代方法是使用 warning 函数,它可以输出消息并继续程序而不会
停止。但是,请确保继续处理是合理的。
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

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
大规模数据分析和建模:基于 Spark 与 R

大规模数据分析和建模:基于 Spark 与 R

Javier Luraschi, Kevin Kuo, Edgar Ruiz
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey

Publisher Resources

ISBN: 9787111656814