Introduction 🐞
- Programming is a complex process, and because it is done by human beings, it often leads to errors.
- Programming errors are called bugs and the process of tracking them down and correcting them is called debugging.
- Use of the term bug to describe small engineering difficulties dates back to at least 1889, when Thomas Edison had a bug with his phonograph.
- Three kinds of errors can occur in a program: syntax errors, runtime errors, and semantic errors
- It is useful to distinguish between them in order to track them down more quickly.
Syntax errors
- Python can only execute a program if the program is syntactically correct; otherwise, the process fails and returns an error message.
- Syntax refers to the structure of a program and the rules about that structure.
- For example, in English, a sentence must begin with a capital letter and end with a period. this sentence contains a syntax error. So does this one
- For most readers, a few syntax errors are not a significant problem, which is why we can read the poetry of E. E. Cummings without problems.
- Python is not so forgiving If there is a single syntax error anywhere in your program, Python will display an error message and quit, and you will not be able to run your program.
- During the first few weeks of your programming career, you will probably spend a lot of time tracking down syntax errors.
- As you gain experience, though, you will make fewer errors and find them faster.
Runtime errors
- The second type of error is a runtime error, so called because the error does not appear until you run the program.
- These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.
Semantic errors
- The third type of error is the semantic error.
- If there is a semantic error in your program, it will run successfully, in the sense that the computer will not generate any error messages, but it will not do the right thing.
- It will do something else specifically, it will do what you told it to do.
- The problem is that the program you wrote is not the program you wanted to write.
- The meaning of the program (its semantics) is wrong.
- Identifying semantic errors can be tricky because it requires you to work backward by looking at the output of the program and trying to figure out what it is doing.