0% found this document useful (0 votes)
30 views12 pages

Exception Handling

Class 12 exceptional handling
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views12 pages

Exception Handling

Class 12 exceptional handling
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Exception Handling

Sometimes while executing a Python program, the program does not

execute at all or the program executes but generates unexpected output or

behaves abnormally. These occur when there are syntax errors, runtime

errors or logical errors in the code. In Python, exceptions are errors that get

triggered automatically.
Errors
Error is an abnormal condition. Whenever it occurs execution of the program is stopped.
Errors are mainly classified into following types:
• Compile Time Errors – Occur at the time of compilation of code.
Further classified into:
⮚ Syntax errors – Occurs when rules of a programming language is misused.
Eg : if a=5:
⮚ Semantics errors – Occurs when statements are not meaningful.
Eg: a+b=c
⮚ Type errors - If a function is given wrong type of data, then type error is assigned by
compiler.
Eg :
a=“Hi”
a**2 This will result in type error.
Errors
• Run Time Errors : A Run time error occurs during execution of the program due to
unexpected situations. Such errors are handled through exception handling routines of
Python.
For example :
• If a program is trying to open a file which does not exist.
• An expression is trying to divide a number by zero are RUN TIME ERRORS.

• Logical Errors : Error which is causes a program to produce incorrect or undesired


output.

Eg:
ctr=1
while(ctr>10):
print(n *ctr)
ctr=ctr+1
Built-in Exceptions in Python
Exception Cause
SyntaxError Raised when there is an error in the syntax of the Python code
ValueError Raised when a built-in method or operation receives an argument that has the right
data type but mismatched or inappropriate values.
IOError Raised when the file specified in a program statement cannot be opened.
KeyboardInterrupt Raised when the user accidentally hits the Delete or Esc key while executing a program
due to which the normal flow of the program is interrupted
ImportError Raised when the requested module definition is not found
EOFError Raised when the end of file condition is reached without reading any data by input().

ZeroDivisionError Raised when the denominator in a division operation is zero


IndexError Raised when the index or subscript in a sequence is out of range
NameError Raised when a local or global variable name is not defined
IndentationError Raised due to incorrect indentation in the program code
Built-in Exceptions in Python
Exception Cause
TypeError Raised when an operator is supplied with a value of incorrect data type

OverFlowError Raised when the result of a calculation exceeds the maximum limit for numeric data
type
KeyError Raised when a mapping (dictionary) key is not found in the set of existing keys
Terminology used within Exception Handling
Terminology Description

Exception An unexpected error that occurs during runtime

try block A set of code that might have an exception thrown in it.

Throwing or raising an error The process by which an exception is generated and passed
to the program

Catching Capturing an exception that has just occurred and executing


statements that try to resolve the problem

Except clause or catch block The block of code that attempts to deal with the exception
Try – Except example
Code Output

Without try – except blocks


Try – Except example with second argument in Except block
Code Output

Message associated with the raised


exception gets printed

Without try – except blocks


Handling Multiple Errors
try:
# ………..
except <exceptionName1> :
# …………….
except <exceptionName2> :
# ……………
except :
# ………….
else :
#If there is no exception then the statements in this block get
executed.
The finally Block

try:
# statements that may raise exception
except:
# handle exception here
finally:
# statements that will always run
Raising Exceptions
Programmers can forcefully raise exceptions in a program using the raise
and assert statements. Once an exception is raised, no further statement in
the current block of code is executed.
• The raise Statement - raise keyword is used to raise/force an exception.
That means, a programmer can force an exception to occur through raise
keyword.
Raising Exceptions
• The assert Statement - In some situations, the programmer has a
clear idea about the requirements and test-conditions required. So in
programs where it is known the likely results of some conditions and
where results being different from the expected results can crash the
programs, Python assert statement can be used if the condition is
resulting as expected or not. In other words, an assert statement in
Python is used to test an expression in the program code. If the result
after testing comes false, then the exception is raised.
print("Enter the Numerator: ")
n = int(input())
print("Enter the Denominator: ")
d = int(input())
assert d != 0, "Denominator must not be 0“
print("n/d =", int(n/d))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy