SlideShare a Scribd company logo
1 of 2
Download to read offline
Using Python: Write a program called ExceptionHandling.py that demonstrates exception
handling. In this program write code using your own data that generates the following errors –
ZeroDivisionError, ValueError, TypeError, and IndexError. Catch all the errors in the main
function. If the error doesn’t fall into any of these types include general Exception to handle
other error types. Your data/code should generate the errors. Do not simulate the errors, the way
we did in the ExceptionChaining program. Generate errors in different functions.
Solution
def div(a,b):
return a/b
def getElement():
list_ = [1]
return list_[1]
def addTwoElement(a,b):
return a+b
def getIntInput():
return int(raw_input('Enter a number : '))
def main():
try:
res = div(a,0)
except ZeroDivisionError:
print 'Cannot divide by zero.'
except:
print 'Unknown error.'
try:
res = addTwoElement(1,'str')
except TypeError:
print 'Please provide correct types while performing addition.'
except:
print 'Unknown error.'
try:
res = getIntInput()
except ValueError:
print 'Cannot convert from string to int.'
except:
print 'Unknown error.'
try:
res = getElement()
except IndexError:
print 'Accessing an element which is out of bound.'
except:
print 'Unknown error.'
main()
OUTPUT:

More Related Content

Similar to Using Python Write a program called ExceptionHandling.py that demon.pdf

C and its errors
C and its errorsC and its errors
C and its errors
Junaid Raja
 
1. Problem StatementWrite a C Program - using if else statements,
1. Problem StatementWrite a C Program - using if else statements, 1. Problem StatementWrite a C Program - using if else statements,
1. Problem StatementWrite a C Program - using if else statements,
TatianaMajor22
 

Similar to Using Python Write a program called ExceptionHandling.py that demon.pdf (20)

Write an algorithm for a program that shows the use of all six math fu.docx
Write an algorithm for a program that shows the use of all six math fu.docxWrite an algorithm for a program that shows the use of all six math fu.docx
Write an algorithm for a program that shows the use of all six math fu.docx
 
Debugging C Console Program Debug the program to identify .pdf
Debugging C Console Program Debug the program to identify .pdfDebugging C Console Program Debug the program to identify .pdf
Debugging C Console Program Debug the program to identify .pdf
 
lecture 15.pptx
lecture 15.pptxlecture 15.pptx
lecture 15.pptx
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As Comp
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As Comp
 
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
 
C and its errors
C and its errorsC and its errors
C and its errors
 
PHP - Introduction to PHP Error Handling
PHP -  Introduction to PHP Error HandlingPHP -  Introduction to PHP Error Handling
PHP - Introduction to PHP Error Handling
 
Python Session - 6
Python Session - 6Python Session - 6
Python Session - 6
 
1. Problem StatementWrite a C Program - using if else statements,
1. Problem StatementWrite a C Program - using if else statements, 1. Problem StatementWrite a C Program - using if else statements,
1. Problem StatementWrite a C Program - using if else statements,
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
Python_UNIT-I.pptx
Python_UNIT-I.pptxPython_UNIT-I.pptx
Python_UNIT-I.pptx
 
Write codeforhumans
Write codeforhumansWrite codeforhumans
Write codeforhumans
 
C programming languag for cse students
C programming languag for cse studentsC programming languag for cse students
C programming languag for cse students
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 
C Theory
C TheoryC Theory
C Theory
 
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 

More from aroraenterprisesmbd

How does a dominant culture differ from a subculture In your answer.pdf
How does a dominant culture differ from a subculture In your answer.pdfHow does a dominant culture differ from a subculture In your answer.pdf
How does a dominant culture differ from a subculture In your answer.pdf
aroraenterprisesmbd
 
Help with implementing the four function the first is an exampleth.pdf
Help with implementing the four function the first is an exampleth.pdfHelp with implementing the four function the first is an exampleth.pdf
Help with implementing the four function the first is an exampleth.pdf
aroraenterprisesmbd
 
Below is the question I need help with. It need to be done in Java. .pdf
Below is the question I need help with. It need to be done in Java. .pdfBelow is the question I need help with. It need to be done in Java. .pdf
Below is the question I need help with. It need to be done in Java. .pdf
aroraenterprisesmbd
 
Css code for a awoko newspaper html website which provides busines.pdf
Css code for a awoko newspaper html website which provides busines.pdfCss code for a awoko newspaper html website which provides busines.pdf
Css code for a awoko newspaper html website which provides busines.pdf
aroraenterprisesmbd
 
Case study Job Satisfaction in the Banking Industry Or the logisti.pdf
Case study Job Satisfaction in the Banking Industry Or the logisti.pdfCase study Job Satisfaction in the Banking Industry Or the logisti.pdf
Case study Job Satisfaction in the Banking Industry Or the logisti.pdf
aroraenterprisesmbd
 

More from aroraenterprisesmbd (20)

In the following list, which term is least related to the others tr.pdf
In the following list, which term is least related to the others  tr.pdfIn the following list, which term is least related to the others  tr.pdf
In the following list, which term is least related to the others tr.pdf
 
If your organization is going to apply for a grant, when do you thin.pdf
If your organization is going to apply for a grant, when do you thin.pdfIf your organization is going to apply for a grant, when do you thin.pdf
If your organization is going to apply for a grant, when do you thin.pdf
 
If the characteristic followed in the pedigree is x-linked recessive .pdf
If the characteristic followed in the pedigree is x-linked recessive .pdfIf the characteristic followed in the pedigree is x-linked recessive .pdf
If the characteristic followed in the pedigree is x-linked recessive .pdf
 
I want to know the purpose of parting lineSolutionA parting li.pdf
I want to know the purpose of parting lineSolutionA parting li.pdfI want to know the purpose of parting lineSolutionA parting li.pdf
I want to know the purpose of parting lineSolutionA parting li.pdf
 
HTML5 & JqueryJavascriptI am trying to code a image slider and I.pdf
HTML5 & JqueryJavascriptI am trying to code a image slider and I.pdfHTML5 & JqueryJavascriptI am trying to code a image slider and I.pdf
HTML5 & JqueryJavascriptI am trying to code a image slider and I.pdf
 
How to access this window in ANSYS (Picture 1 contains analysis s.pdf
How to access this window in ANSYS (Picture 1 contains analysis s.pdfHow to access this window in ANSYS (Picture 1 contains analysis s.pdf
How to access this window in ANSYS (Picture 1 contains analysis s.pdf
 
How and when do we learn ethicsHow and when do we learn ethics.pdf
How and when do we learn ethicsHow and when do we learn ethics.pdfHow and when do we learn ethicsHow and when do we learn ethics.pdf
How and when do we learn ethicsHow and when do we learn ethics.pdf
 
How does a dominant culture differ from a subculture In your answer.pdf
How does a dominant culture differ from a subculture In your answer.pdfHow does a dominant culture differ from a subculture In your answer.pdf
How does a dominant culture differ from a subculture In your answer.pdf
 
Help with implementing the four function the first is an exampleth.pdf
Help with implementing the four function the first is an exampleth.pdfHelp with implementing the four function the first is an exampleth.pdf
Help with implementing the four function the first is an exampleth.pdf
 
Encrypt the message HALT by translating the letters into numbe.pdf
Encrypt the message  HALT  by translating the letters into numbe.pdfEncrypt the message  HALT  by translating the letters into numbe.pdf
Encrypt the message HALT by translating the letters into numbe.pdf
 
Each letter in the word Massachusetts is written on a card. The card.pdf
Each letter in the word Massachusetts is written on a card. The card.pdfEach letter in the word Massachusetts is written on a card. The card.pdf
Each letter in the word Massachusetts is written on a card. The card.pdf
 
Below is the question I need help with. It need to be done in Java. .pdf
Below is the question I need help with. It need to be done in Java. .pdfBelow is the question I need help with. It need to be done in Java. .pdf
Below is the question I need help with. It need to be done in Java. .pdf
 
Css code for a awoko newspaper html website which provides busines.pdf
Css code for a awoko newspaper html website which provides busines.pdfCss code for a awoko newspaper html website which provides busines.pdf
Css code for a awoko newspaper html website which provides busines.pdf
 
Case study Job Satisfaction in the Banking Industry Or the logisti.pdf
Case study Job Satisfaction in the Banking Industry Or the logisti.pdfCase study Job Satisfaction in the Banking Industry Or the logisti.pdf
Case study Job Satisfaction in the Banking Industry Or the logisti.pdf
 
You are constructing working models of the heart for extra credit You.pdf
You are constructing working models of the heart for extra credit You.pdfYou are constructing working models of the heart for extra credit You.pdf
You are constructing working models of the heart for extra credit You.pdf
 
why does countercurrent flow yields greater oxygen uptake than concu.pdf
why does countercurrent flow yields greater oxygen uptake than concu.pdfwhy does countercurrent flow yields greater oxygen uptake than concu.pdf
why does countercurrent flow yields greater oxygen uptake than concu.pdf
 
Wind Shear can be defined in the horizontal plane as well as the vert.pdf
Wind Shear can be defined in the horizontal plane as well as the vert.pdfWind Shear can be defined in the horizontal plane as well as the vert.pdf
Wind Shear can be defined in the horizontal plane as well as the vert.pdf
 
While hiking on some small islands in Lake Superior, you stumble acr.pdf
While hiking on some small islands in Lake Superior, you stumble acr.pdfWhile hiking on some small islands in Lake Superior, you stumble acr.pdf
While hiking on some small islands in Lake Superior, you stumble acr.pdf
 
Which phyla have megaphylls Bryophyta Monilophyta Coniferophyta .pdf
Which phyla have megaphylls  Bryophyta  Monilophyta  Coniferophyta  .pdfWhich phyla have megaphylls  Bryophyta  Monilophyta  Coniferophyta  .pdf
Which phyla have megaphylls Bryophyta Monilophyta Coniferophyta .pdf
 
Where do endocyotic vesicles come fromA. Plasma membranceB. Nuc.pdf
Where do endocyotic vesicles come fromA. Plasma membranceB. Nuc.pdfWhere do endocyotic vesicles come fromA. Plasma membranceB. Nuc.pdf
Where do endocyotic vesicles come fromA. Plasma membranceB. Nuc.pdf
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 

Using Python Write a program called ExceptionHandling.py that demon.pdf

  • 1. Using Python: Write a program called ExceptionHandling.py that demonstrates exception handling. In this program write code using your own data that generates the following errors – ZeroDivisionError, ValueError, TypeError, and IndexError. Catch all the errors in the main function. If the error doesn’t fall into any of these types include general Exception to handle other error types. Your data/code should generate the errors. Do not simulate the errors, the way we did in the ExceptionChaining program. Generate errors in different functions. Solution def div(a,b): return a/b def getElement(): list_ = [1] return list_[1] def addTwoElement(a,b): return a+b def getIntInput(): return int(raw_input('Enter a number : ')) def main(): try: res = div(a,0) except ZeroDivisionError: print 'Cannot divide by zero.' except: print 'Unknown error.' try: res = addTwoElement(1,'str') except TypeError: print 'Please provide correct types while performing addition.' except: print 'Unknown error.' try: res = getIntInput()
  • 2. except ValueError: print 'Cannot convert from string to int.' except: print 'Unknown error.' try: res = getElement() except IndexError: print 'Accessing an element which is out of bound.' except: print 'Unknown error.' main() OUTPUT: