of Errors
Type:
We always try to create an error free code so that uninterrupted exe’
a) Syntaa error
b) Logical error
\e error
rect result. But, even after a lot of our efforts, some erro!
sscnung a code. These errors can be classified into the following three categories:
cution could be led to
rs might occur while—
Chapter 2~ Revision of Python Prog
anne
tax error
(a) Symi occurs When the syntax or the grammar of the Programming statem
This sowed properly. Such programming errors st
‘not
i 'ypically involve incorrect
sequence, undefined terms or m
punctuation,
incorrect worl ing parenthesis,
For example:
ius refer a statement written in Python:
p = length + breadth)
se gual is mathematically correct but syntactically wi fe is
jpis statement is mal r ut syntactically wrong for a Python inte .
The reason forthe syntax error is simply missing sign between 2and (length veer
‘The correct statement will be: .
Let
Pp = 2*(ength + breadth)
Now, the above statement is correct and it will produce the result when executed,
(b) Logical error
in the planning of th —— ;
Logical error is an error in the planning of the program’s logic. It may be im
sequencing of the statements or incorrect use of the operators, The problem may bettie to
wirong logic and thus it does not produce the desired output
For example:
You want to find the sum of two numbers but the instruction in Python code is given as:
a,b= 15,28
ceath
print((’Sum of two numbers=", ¢)
This snippet will print the product of ‘a! and 'b’' whereas, you wanted to find the sum of
‘a and 'b’. Hence, this is a logical error,
(c) Runtime error
There may be some mistakes other than syntax error or logical error which encounters
execution, is referred to as Runtime errors. These errors may arise due to instruction given
as dividing a number by zero or finding the square root of a negative number, etc,
For example:
‘The expression a = (b + ¢)/d
If the value of d = 0, it will show as ZeroDivisionError, which is a runtime ertor,
Exception
Itis important to know that even if a statement or expression is syntactically correct, an
etror may arise during its execution.
For example,
If you are trying to open a file that does not exist or performing a division by zero and so
on, These types of errors may interrupt the normal execution of the code that are called
exceptions. An exception refers to as the Python object that represents an error. When an
error occurs during the execution of a program, an exception is said to have been raised.
exceptions need to be handled by the programmer so that the code does not terminate
abnormally.
Following are some meaningful terms while designing a code and may anticipate such
erroneous situations during its execution.
* Throwing an excepti
pes ion handlers are
Throwing an exception means raising an exception. In Python, excep’
a err-
® w:20004 ct Computer seen wah Python
designed to execute when a specific
statement in the current block will be
ception is raised. Once exception is raised, y,
couted
* Catching an exception
Catching an exception means the handling of
An exception is said te be eaught when a code
is executed Hany exception fx caught i the (ar block th
block
an exception by the exception handier,
designed to handle a specific exc
wen it is handled in the ev
ie
n
sing ty
yyy aeode, HF the use
ati blow
Catching exceptions
While writing or debuy
ot the code then codes ane put inside
fallowedt by any / Boek to manage the exceptions:
xperiencen an exception In-a particular pay
k. It must be noted that every 117 block is,
ervstand the concept with the help of a code,
Let us ung
# a code to illustrate try - catch block
try:
| meint(input(’Enter numerator
| nsint(input(’Enter denominator”)
| n/n)
| print(Quotient:",ans)
|
|
print(’Division performed successfully") |
except ZeroDivisionError:
print("Division not possible as the valu
note @,
While executing the code, if an exceptior encountered, no statement in the current block
will be executed and the contro! will be transferred to the except block.
2 of denominator is Zerol!!")
Output of the code:
DB iyten 381 shel =o x
Fie Edit Shel_Ocbug Options Window Helo
ee elena wi-B-1rib?8ab6, Doc 18 219, 22535=24) [HSE v.TOLe 32 BEE Catal} 4
jon wins?
Joo iam on, scopyeight™, “eradits” or “License()” for wore information.
S35
po aesrant: Cx/Users/DiLip Kumar Dey/Desktop/trycat.ch.py —a——
lenter amerator:12
lenter denoninator:0
eae scone poaaile ag the value of denominator is Zoro
>>>.
p2?____ nesrant: c1/Usere/DLLip Kumar Dey/Desktop/trycatch.py s=mene—————=
Enter nunerator: 12
lenter denominator: 5
Jovotient: 2.4
Division performed succesofully
b> |
inte co
Built-in Exception Messages
Exception means errors that users may come across while executing a code. Removing errors
handling. The users must resolve all the errors from
from a code is referred to as exception
their code for its successful execution. To handle exception easily, you must know the errora _
Ghapler 2 Rovnion of Python Programming it
at appear on the screen during execution of a corde. §
@
cages th’
rome of the errey essage:
messaey yy their meanings have been listed below: mmo
alot
et ror occurs during rutin why ay
expression oF a va
getting divided by p ora value is unknowingly
|For example:
“Ife = 0 then p=(arb)/e will result in (7%
ODI visionError’
When an identifier used in the expression does not find j
run time. *s Not find its value during,
For exaniple:
p=(a+b)/c then it will result in 'NameFrror’ when one or more variables
are not assigned values. °
——Saor_| This exception her
= sption occurs when there is a mismatch in dat duri
Typeee assignment or comparison, aeapes during
For example:
p=(a+b)/c then it will result in “TypeError' when the variables a, b and |
care of different data types (say, int and string).
ea When the given value is not suitable to perform an operation.
For example:
p=(a+b)/math.sqrt(c) then it will result in "Value Error’ when ¢ is
assigned a negative number.
TndexError It occurs when index of a sequence is out of the range. |
KeyError It is raised when a key is not found or doesn't exist in a dictionary.
MemoryError This error occurs when a program runs out of memory. It means that |
the program has created many objects that can't be accumulated in |
the memory. |
'o or It will occur when the result of an operation is too large to fit in the
| OverflowEsro: pel "Bi
| memory. |
fe