Intro to Errors Flashcards
Error
When a program does unexpected things
Crash
When a program comes to a halt because of an error
Syntax Error
An error caused by incorrect syntax that Python cannot interpret
Runtime Error
An error raised during runtime
Logical Error
An error caused by incorrect logic
What could cause a Syntax error?
Syntax errors are often caused by things such as:
missing a keyword missing a colon missing the end of a brace, like ), }, ], etc misspelling something accessing a list or dictionary with the wrong syntax unmatching indentation levels mixed-use of tabs or spaces
What could cause a Runtime Error?
A runtime error is a kind of error that occurs while our program is already running.
There are actually plenty of actions that Python programs can’t do:
dividing by zero performing an operation on incompatible types using an identifier which has not been defined accessing a list element, dictionary value or object attribute which doesn’t exist trying to access a file which doesn’t exist
When our Python program is running, and it tries to do one of those actions, Python will raise a runtime error.
What could cause a logical error?
A logical error is a kind of error that occurs when a program produces an incorrect result or outcome.
For example, if we write a Python program that should count the number of letters in the string “Grace Hopper”, and it produces the number 13 instead of the correct 12, and there are no syntax or runtime errors, then we have a logical error.
Logical errors can happen for an infinite number of reasons, but here are some common causes:
using the wrong variable name incorrect indentation not having the correct conditional checks off-by-one errors
Stack Trace (or “Trace Back” in Python)
A report of the active stack frames at a certain point in time during the execution of a program. Reports error messages and details about that error
Stacktrace is a list of what was running when it encountered the error with details about what happened.
Standard Stream Output
Let’s consider briefly where we see error messages, and in general all output. Right now, when we run Python code from the terminal, we see the output in the terminal. That’s because our computers and terminals have configured a standard stream for output , or an “output channel.” The output channel, or standard stream for output, for the terminal is the terminal itself by default.
We introduce this idea briefly because one day, the standard stream may change. :)In computer programming, standard streams are interconnected input and output communication channels[1] between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console (input via keyboard, output via monitor), but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection or a pipeline. More generally, a child process inherits the standard streams of its parent process.
Traceback in Python
This module provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter.
The module uses traceback objects — this is the object type that is stored in the sys.last_traceback variable and returned as the third item from sys.exc_info().
The module defines a variety of functions that you can look up in python library.
Finding The Good Stuff: How to Read Error Messages
When we come across errors, we should take notes of three things:
1) What is the description of the error
2) What is the name of the error
3) What is the line of code that caused the error, its file name and line number