Errors in Python | CBSE Class 11 | Computer Science
Dear Class 11th STUDENTS, ! Welcome to this tutorial of Errors in Python from your CBSE class 11 of Computer Science Syllabus .
In this tutorial, we shall be learning our chapter-6: Errors in Python from Unit 2: Programming and Computational Thinking (PCT-1) as CBSE BOARD suggested to learn about computer system and its organisation to complete this section.
Unit 2: Programming and Computational Thinking (PCT-1)
Chapter 6 : Errors in python:
- Syntax errors
- Logical errors
- Run-time errors
I advice you to check the latest syllabus given by CBSE Board at its Official website: www.cbseacademic.nic.in
Also, in this tutorial we will covers all necessary topics/concepts required to complete your exams preparations in CBSE classes 11th.
NOTE:
- We are also giving some important Questions & Answers for better understanding as well as preparation for your examinations.
- You may also download PDF file of this tutorial from our SHOP for free.
- For your ease and more understanding, we are also giving the video explanation class of each and every topic individually, so that you may clear your topics and get success in your examinations.
Introduction to Errors in Python:
An error, sometimes called ‘a bug’, is anything in the code that prevents a program from compiling and running correctly. Some program bugs are catastrophic in their effects, while others are comparatively less harmful and still others are so unclear that you will ever discover them. There are broadly three types of errors: Compile-time errors, run-time errors and logical errors.
Types of Errors in Python:
Compile-Time Errors
Errors that occur during compile-time, are compile-time errors. When a program compiles, its source code is checked for whether it follows the programming language’s rules or not. Two types of errors fall into category of compile-time errors.
Syntax Errors
Syntax errors occur when rules of a programming language are misused i.e., when a grammatical rule of Python is violated. For example, observe the following two statements:
X<-YZ
if X = (X *Y)
These two statements will result in syntax errors as ‘<-‘ is not an assignment operator in Python and is the assignment operator, not a relational operator. Also, if is a block statement, it requires a colon (:) at the end of it, which is missing above.
Therefore, the correct statements will be as follows:
X = Y Z
if X == (X + Y) :
One should always try to get the syntax right the first time, as a syntax error wastes computing time and money, as well as programmer’s time and it is preventable.
Semantics Errors
Semantics Errors occur when statements are not meaningful. For instance, the statement ‘Sita plays Guitar’ is syntactically and semantically correct as it has some meaning but the statement Similarly, there are semantics rules of a programming language, violation of which results in ‘Guitar plays Sita’ is syntactically correct (as the grammar is correct) but semantically incorrect
semantical errors. For instance, the statement
X * Y Z
will result in a semantical error as an expression cannot come on the left side of an assignment statement
Logical Errors
Sometimes, even if you don’t encounter any error during compile-time and run-time, your program does not provide the correct result. This is because of the programmer’s mistaken analysis of the problem he or she is trying to solve. Such errors are logical errors. For instance, an incorrectly implemented algorithm, or use of a variable before its initialization, or unmarked end for a loop, or wrong parameters passed are often the hardest to prevent and to locate. These must be handled carefully. Sometimes logical errors are treated as a subcategory of run-time errors. 8.7.10 Run-Time Errors
Run-time errors:
Errors that occur during the execution of a program are run-time errors. These are harder to detect errors. Some run-time errors stop the execution of the program which is then called program “crashed” or “abnormally terminated”.
Most run-time errors are easy to identify because program halts when it encounters them eg., an infinite loop or wrong value (of different data type other than required) is input. Normally, programming languages incorporate checks for run-time errors, so does Python. However, Python usually takes care of such errors by terminating the program, but a program that crashes whenever it detects an error condition is not desirable. Therefore, a program should be robust so as to recover and continue following an error.
Exceptions
Errors and exceptions are similar but different terms. While error represents, any bug in the code that disrupts running of the program or causes improper output, an Exception refers to any irregular situation occurring during execution/run-time, which you have no control on. Errors in a program can be fixed by making corrections in the code, fixing exceptions is not that simple. Let us try to understand the difference between an error and exception with the help of real life
example. if you operate an ATM then
- entering wrong account number or wrong pin number is an ERROR.
- ‘not that much amount in account’ is an EXCEPTION.
- ‘ATM machine struck’ is also an EXCEPTION.
So you can think of Exceptions in a program as a situation that occurs during runtime and you have no control on it e.g., you write a code that opens a data file and displays its contents on screen. This program’s code is syntactically correct and logically correct too. But when you run this program, the file that you are opening, does not exist on disk – So the program has no errors but an exception occurred.