SlideShare a Scribd company logo
1 of 4
Builtin exception examples
1) ZeroDivisionError (It is raised when number is divided by 0)
a=int(input())
b=int(input())
try:
c=a/b
print(c)
except ZeroDivisionError:
print("b cant be 0")
2) ValueError (It is raised when the arguments passed to the function are of invalid data
type)
Example 1:
try:
a=int(input())
print(a)
except ValueError:
print("Enter integer data only")
Example 2:
import math
try:
print(math.sqrt(-1))
except ValueError:
print("sqrt() must always accept valid type as an argument")
3) NameError (It is raised when identifier is not found in local or global
namespace)
try:
print(b)
except NameError:
print("b is not defined")
4) IndexError (It is raised when the index is not found in the sequence or index is
out of range)
try:
s="abcd"
print(s[4])
except IndexError:
print("index out of range")
5) KeyError (It is raised when key is not found in the dictionary)
try:
d={100:"Raju",101:"Ravi"}
print(d[102])
except KeyError:
print("Key is not present in dictionary")
6) KeyboardInterrupt (it is raised when user interrupts program execution by
pressing ctrl+c)
try:
a=int(input())
print(a)
except KeyboardInterrupt:
print("program execution has been terminated by pressing ctrl-c")
7) TypeError (it is raised when invalid operation is performed among different types)
try:
s="abcd"/5
print(s)
except TypeError:
print("/ operation is not possible for strings")
8) IOError (it is raised when input or output operation fails)
try:
f=open("file.txt","r")
print(f.read())
except IOError:
print("file does not exist for reading")
9) OverflowError (it is raised when maximum limit of numeric type is exceeded during
calculation)
try:
import math
print(math.pow(2000,1456))
except OverflowError:
print("result is out of range")
10) AttributeError (it is raised when attribute reference fails)
try:
l=[1,2,3]
l.add(4)
except AttributeError as e:
print(e) #prints 'list' object has no attribute 'add'
11) ImportError (it is raised when import statement fails)
try:
import koushik
except ImportError as e:
print(e) #No module named 'koushik'
12) UnboundLocalError (it is raised when attempt is made to access local variable in
function when no value has been assigned to it)
a=20
def f():
try:
a=a+1
print(a)
except UnboundLocalError:
print("local variable a is not defined any value but it is referenced")
f()

More Related Content

What's hot

2.3 graphs of functions
2.3 graphs of functions2.3 graphs of functions
2.3 graphs of functions
hisema01
 
3.2 exponential function tables
3.2 exponential function tables3.2 exponential function tables
3.2 exponential function tables
hisema01
 
Dti2143 lab sheet 9
Dti2143 lab sheet 9Dti2143 lab sheet 9
Dti2143 lab sheet 9
alish sha
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
Syed Umair
 

What's hot (19)

Function basics
Function basicsFunction basics
Function basics
 
Assignment
AssignmentAssignment
Assignment
 
2. Базовый синтаксис Java
2. Базовый синтаксис Java2. Базовый синтаксис Java
2. Базовый синтаксис Java
 
C++17 not your father’s c++
C++17  not your father’s c++C++17  not your father’s c++
C++17 not your father’s c++
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
2.3 graphs of functions
2.3 graphs of functions2.3 graphs of functions
2.3 graphs of functions
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5
 
Learn How to create pyramids in c
Learn How to create pyramids in cLearn How to create pyramids in c
Learn How to create pyramids in c
 
SATySFiのこれからの課題たち
SATySFiのこれからの課題たちSATySFiのこれからの課題たち
SATySFiのこれからの課題たち
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
programming for Calculator in java
programming for Calculator in javaprogramming for Calculator in java
programming for Calculator in java
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
CS50 Lecture4
CS50 Lecture4CS50 Lecture4
CS50 Lecture4
 
Clojure lang
Clojure langClojure lang
Clojure lang
 
SPL 11.1 | Problems on Loop , Nested Loop
SPL 11.1 | Problems on Loop , Nested LoopSPL 11.1 | Problems on Loop , Nested Loop
SPL 11.1 | Problems on Loop , Nested Loop
 
PYTHON REVISION TOUR - 1
PYTHON REVISION TOUR - 1PYTHON REVISION TOUR - 1
PYTHON REVISION TOUR - 1
 
3.2 exponential function tables
3.2 exponential function tables3.2 exponential function tables
3.2 exponential function tables
 
Dti2143 lab sheet 9
Dti2143 lab sheet 9Dti2143 lab sheet 9
Dti2143 lab sheet 9
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 

Similar to Exception Example in Python

Python-3-compiled-Cheat-Sheet-version-3.pdf
Python-3-compiled-Cheat-Sheet-version-3.pdfPython-3-compiled-Cheat-Sheet-version-3.pdf
Python-3-compiled-Cheat-Sheet-version-3.pdf
letsdism
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
info30292
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 

Similar to Exception Example in Python (20)

Python-3-compiled-Cheat-Sheet-version-3.pdf
Python-3-compiled-Cheat-Sheet-version-3.pdfPython-3-compiled-Cheat-Sheet-version-3.pdf
Python-3-compiled-Cheat-Sheet-version-3.pdf
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
 
Kotlin Starter Pack
Kotlin Starter PackKotlin Starter Pack
Kotlin Starter Pack
 
Exceptional exceptions
Exceptional exceptionsExceptional exceptions
Exceptional exceptions
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
 
Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmers
 
C++11
C++11C++11
C++11
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189
 
Type hints Python 3
Type hints  Python 3Type hints  Python 3
Type hints Python 3
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Golang dot-testing
Golang dot-testingGolang dot-testing
Golang dot-testing
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
 
Why Learn Python?
Why Learn Python?Why Learn Python?
Why Learn Python?
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 

Recently uploaded

Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Recently uploaded (20)

Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 

Exception Example in Python

  • 1. Builtin exception examples 1) ZeroDivisionError (It is raised when number is divided by 0) a=int(input()) b=int(input()) try: c=a/b print(c) except ZeroDivisionError: print("b cant be 0") 2) ValueError (It is raised when the arguments passed to the function are of invalid data type) Example 1: try: a=int(input()) print(a) except ValueError: print("Enter integer data only") Example 2: import math try: print(math.sqrt(-1)) except ValueError: print("sqrt() must always accept valid type as an argument") 3) NameError (It is raised when identifier is not found in local or global namespace) try:
  • 2. print(b) except NameError: print("b is not defined") 4) IndexError (It is raised when the index is not found in the sequence or index is out of range) try: s="abcd" print(s[4]) except IndexError: print("index out of range") 5) KeyError (It is raised when key is not found in the dictionary) try: d={100:"Raju",101:"Ravi"} print(d[102]) except KeyError: print("Key is not present in dictionary") 6) KeyboardInterrupt (it is raised when user interrupts program execution by pressing ctrl+c) try: a=int(input()) print(a) except KeyboardInterrupt: print("program execution has been terminated by pressing ctrl-c") 7) TypeError (it is raised when invalid operation is performed among different types)
  • 3. try: s="abcd"/5 print(s) except TypeError: print("/ operation is not possible for strings") 8) IOError (it is raised when input or output operation fails) try: f=open("file.txt","r") print(f.read()) except IOError: print("file does not exist for reading") 9) OverflowError (it is raised when maximum limit of numeric type is exceeded during calculation) try: import math print(math.pow(2000,1456)) except OverflowError: print("result is out of range") 10) AttributeError (it is raised when attribute reference fails) try: l=[1,2,3] l.add(4) except AttributeError as e:
  • 4. print(e) #prints 'list' object has no attribute 'add' 11) ImportError (it is raised when import statement fails) try: import koushik except ImportError as e: print(e) #No module named 'koushik' 12) UnboundLocalError (it is raised when attempt is made to access local variable in function when no value has been assigned to it) a=20 def f(): try: a=a+1 print(a) except UnboundLocalError: print("local variable a is not defined any value but it is referenced") f()