Errors
Beginning programmers make mistakes writing programs because of inexperience in programming in general, or due to unfamiliarity with a programming…
Four kinds of errors
Beginning programmers make mistakes writing programs because of inexperience in programming in general, or due to unfamiliarity with a programming language.There are four general kinds of errors: syntax errors, run-time errors, overflow errors and logic errors.
The interpreter reads the Python source file and translates it into an executable form. This is the translation phase. If the interpreter detects an invalid program statement during the translation phase, it will terminate the program's execution and report an error.
Syntax errors
Such errors result from the programmer's misuse of the language. A syntax error is a common error that the interpreter can detect when attempting to translate a Python statement into machine language.Run-time errors
A syntactically correct Python program still can have problems. Some language errors depend on the context of the program's execution. Such errors are called run-time exceptions or run-time errors.We say the interpreter raises an exception. Run-time exceptions arise after the interpreter's translation phase and during the program's execution phase. Run-time errors will cause the program to immediately terminate.
Here's another example of a run-time error.
The expression dividend/divisor is potentially dangerous. If the user enters, for example, 32 and 4, the program works nicely. If the user instead types the numbers 32 and 0, the program reports an error and terminates. Division by zero is undefined in mathematics, and division by zero in Python is illegal.
Overflow errors
Another example of a run-time error is an overflow error. An overflow error is an error that occurs when a computer attempts to handle a number that is outside of the defined range of values.Logic errors
The interpreter can detect syntax errors during the translation phase and uncover run-time exceptions during the execution phase. Both kinds of problems represent violations of the Python language. Such errors are the easiest to repair, because the interpreter indicates the exact location within the source code where it detected the problem.A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly. A logic error is subtle and will not cause the program to terminate.
Consider the effects of replacing the expression dividend/divisor with divisor/dividend.
The program runs, and unless the user enters a value of zero for the dividend, the interpreter will report no errors. However, the answer it computes is not correct in general. The only time the program will print the correct answer is when dividend equals divisor.
The program contains an error, but the interpreter is unable to detect the problem. An error of this type is known as a logic error.