Every programmer encounters errors, both those who are just beginning, and those who have been programming for years. Encountering errors and exceptions can be very frustrating at times, and can make coding feel like a hopeless endeavour. However, understanding what the different types of errors are and when you are likely to encounter them can help a lot. Once you know why you get certain types of errors, they become much easier to fix. Errors in Python have a very specific form, called a traceback. Let’s examine one:
This particular traceback has two levels. You can determine the number of levels by looking for the number of arrows on the left hand side. In this case:
The last level is the actual place where the error occurred. The other level(s) show what function the program executed to get to the next level down. So, in this case, the program first performed a
function call to the function
So what error did the program actually encounter? In the last line of the traceback, Python helpfully tells us the category or type of error (in this case, it is an If you encounter an error and don’t know what it means, it is still important to read the traceback closely. That way, if you fix the error, but encounter a new one, you can tell that the error changed. Additionally, sometimes knowing where the error occurred is enough to fix it, even if you don’t entirely understand the message. If you do encounter an error you don’t recognize, try looking at the official documentation on errors. However, note that you may not always be able to find the error there, as it is possible to create custom errors. In that case, hopefully the custom error message is informative enough to help you figure out what went wrong. Syntax ErrorsWhen you forget a colon at the end of a line, accidentally add one space too many when indenting under an People can typically figure out what is meant by text with no punctuation, but people are much smarter than computers. If Python doesn’t know how to read the program, it will give up and inform you with an error. For example:
Here, Python tells us that there is a Actually, the function above has two issues with syntax. If we fix the problem with the colon, we see that there is also an
Both
Variable Name ErrorsAnother very common type of error is
called a
Variable name errors come with some of the most informative error messages, which are usually of the form “name ‘the_variable_name’ is not defined”. Why does this error message occur? That’s a harder question to answer, because it depends on what your code is supposed to do. However, there are a few very common reasons why you might have an undefined variable. The first is that you meant to use a string, but forgot to put quotes around it:
The second reason is that you might be trying to use a variable that does not yet exist. In the following example,
Finally, the third possibility is that you made a typo when you
were writing your code. Let’s say we fixed the error above by adding the line
Index ErrorsNext up are errors having to do with containers (like lists and strings) and the items within them. If you try to access an item in a list or a string that does not exist, then you will get an error. This makes sense: if you asked someone what day they would like to get coffee, and they answered “caturday”, you might be a bit annoyed. Python gets similarly annoyed if you try to ask it for an item that doesn’t exist:
Here, Python is telling us that there is an File ErrorsThe last type of error we’ll cover today are those associated with reading and writing files:
One reason for receiving this error is that you specified an incorrect path to the file. For example, if I am currently in a folder called A related issue can
occur if you use the “read” flag instead of the “write” flag. Python will not give you an error if you try to open a file for writing when the file does not exist. However, if you meant to open a file for reading, but accidentally opened it for writing, and then try to read from it, you will get an
These are the most common errors with files, though many others exist. If you get an error that you’ve never seen before, searching the Internet for that error type often reveals common reasons why you might get that error.
Which of the following terms describes an attempt to read a variable that stores a null value?Which of the following terms describes an attempt to read a variable that stores a null value? A NULL pointer dereference occurs when the application dereferences a pointer that it expects to be valid, but is NULL, typically causing a crash or exit.
What is the best countermeasure against social engineering correct answer?Very broadly, one of the best counter measures in regards to social engineering involves educating your employees about what it is, why it's hazardous, and how to avoid social engineers. There are multiple ways in which to educate employees.
Which of the following is an example of a request forgery malware?Which of the following is an example of a request forgery malware? The SQL injection inserts specially created structured query language statements to manipulate the database server, giving control of the database to the attacker, who can then manipulate the database.
Which of the following is a common form of social engineering attack quizlet?Which of the following is a common form of social engineering attack? Hoax virus information e-mails. Which of the following is not not a form of social engineering? You have just received a generic-looking email that is addressed as coming from the administrator of your company.
|