The learning objectives of this section are: Show
What is an error?Errors most often occur when code is used in a way that it is not intended to be used. For example adding two strings together produces the following error:
The In R there are two other constructs which are related to errors: warnings and messages. Warnings are meant to indicate that something seems to have gone wrong in your program that should be inspected. Here’s a simple example of a warning being generated:
The Messages simply print to the R console, though they are generated by an underlying mechanism that is similar to how errors and warning are generated. Here’s a small function that will generate a message:
Generating ErrorsThere are a few essential functions for generating errors, warnings, and messages in R. The
If an error occurs inside of a function then the name of that function will appear in the error message:
The
The
Just like errors, a warning generated inside of a function will include the name of the function in which it was generated:
Messages are simpler than errors or warnings; they just print strings to the R console. You can issue a message with the
When to generate errors or warningsStopping the execution of your program with You can think
of a function as kind of contract between you and the user: if the user provides specified arguments, your program will provide predictable results. Of course it’s impossible for you to anticipate all of the potential uses of your program, so the results of executing a function can only be predictable with regard to the type of the result. It’s appropriate to create a warning when this contract between you and the user is violated. A perfect example of this situation is the result of R has largely been developed according to the Unix Philosophy (which is further discussed in Chapter 3), which generally discourages printing text to the console unless something unexpected has occurred. Languages that commonly run on Unix systems like C, C++, and Go are rarely used interactively, meaning that they usually underpin computer infrastructure (computers “talking” to other computers). Messages printed to the console are therefore not very useful since nobody will ever read them and it’s not straightforward for other programs to capture and interpret them. In contrast R code is frequently executed by human beings in the R console, which serves as an interactive environment between the computer and person at the keyboard. If you think your program should produce a message, make sure that the output of the message is primarily meant for a human to read. You should avoid signaling a condition or the result of your program to another program by creating a message. How should errors be handled?Imagine writing a program that will take a long time to complete because of a complex calculation or because you’re handling a large amount of data. If an error occurs during this computation then you’re liable to lose all of the results that were calculated before the error, or your program may not finish a critical task that a program further down your pipeline is depending on. If you anticipate the possibility of errors occurring during the execution of your program then you can design your program to handle them appropriately. The Let’s construct a simple function I’m going to call
This
function takes an expression as an argument and tries to evaluate it. If the expression can be evaluated without any errors or warnings then the result of the expression is returned and the message
Notice that we’ve effectively transformed errors and warnings into messages. Now that you know the basics of generating and catching errors you’ll need to decide when your program should generate an error. My advice to you is to limit the number of errors your program generates as much as possible. Even if you design your program so that it’s able to catch and handle errors, the error handling process slows down your program by orders of magnitude. Imagine you wanted to write a simple function that checks if an argument is an even number. You might write the following:
You can see that providing a string causes this function to raise an error. You could imagine though that you want to use this function across a list of different data types, and you only want to know which elements of that list are even numbers. You might think to write the following:
This appears to be working the way you intended, however when applied to more data this function will be seriously
slow compared to alternatives. For example I could check that
I> Notice that by using To demonstrate the difference in the speed of the code we’ll use the
The error catching approach is nearly 15 times slower! Proper error handling is an essential tool for any software developer so that you can design programs that are error tolerant. Creating clear and informative error messages is essential for building quality software. One closing tip I recommend is to put documentation for your software online, including the meaning of the errors that your software can potentially throw. Often a user’s first instinct when encountering an error is to search online for that error message, which should lead them to your documentation! Summary
Which type of error causes a program to run but provide incorrect or unexpected results?A logic error in a program is any code that causes incorrect output/results even though the program runs to completion. A program with a logic error may give the correct answer sometimes and the wrong answer other times. Logic errors typically are the most difficult type of errors to find and correct.
What is an error in a program called quizlet?Errors in a program are called hacks. Tap the card to flip 👆 Definition. 1 / 20.
Which loop can be used for input validation ____?Often it is necessary to validate data input by the user, and repeat the request for the data in the case where the input of the user is not valid. This can be done by using a do loop.
What is the most difficult input error to validate?The most difficult input error to validate is an empty read. An input validation loop is sometimes called an error handler.
|