SlideShare a Scribd company logo
Exception Handling
in Python
What is an Exception?
In Python, an exception is an event
that occurs during the execution of a
program that disrupts the normal
flow of the program's instructions.
Why exceptions occur in
programs?
Code errors: A software engineer may make a mistake, such as
using a null resource or passing the wrong parameter to a function
Invalid user input: The user may input something that is invalid
or unacceptable
Device failure: A system failure can occur, which is a run-time
error that cannot be handled programmatically
Arithmetic errors: A program may make a mistake in a
mathematical calculation or operation
Importance of Exception
Handling
Exception handling is important because it allows programs to handle errors that occur
during execution. It's especially important in the following situations:
•Handling errors
Exception handling allows programs to distinguish between normal and erroneous return values. This is
especially important in geospatial programming, where issues like missing files or corrupt data can occur.
Separating error-handling code
Exception handling separates the details of what to do when something unexpected happens from the main logic of a
program
Handling concurrency
In multi-threaded applications, exceptions thrown by one thread can propagate to other threads, causing the application to
crash. Exception handling can prevent this from happening.
Common Python Exceptions
The TRY block lets you test a block of code for
error.
The EXCEPT block lets you handle the error.
Syntax:
try: # Code that may cause an exception except
ExceptionType: # Code to handle the exception
The try-except Block
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
The try-except Block
try:
even_numbers = [2,4,6,8]
print(even_numbers[5])
except ZeroDivisionError:
print("Denominator cannot be 0.")
except IndexError:
print("Index Out of Bound.")
# Output: Index Out of Bound
Multiple except
Block
try:
num = int(input("Enter a
number: ")) num % 2 == 0
except:
print("Not an even number!")
else:
print("its a even number")
The else Clause

More Related Content

PPTX
Exception handling with python class 12.pptx
PPT
Exception handling
PPTX
Chapter 13 exceptional handling
PDF
java programm for beginners (basic) level
PPTX
JAVA Presenttation topics Programs.pptx
PPT
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
PPTX
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Exception handling with python class 12.pptx
Exception handling
Chapter 13 exceptional handling
java programm for beginners (basic) level
JAVA Presenttation topics Programs.pptx
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx

Similar to Exception Handling in Python topic .pptx (20)

PPT
Exception handling
PPT
S D D Program Development Tools
PDF
lecs101.pdfgggggggggggggggggggddddddddddddb
PPTX
Exceptions overview
PPT
exception-handling-in-java.ppt
PDF
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
PPT
Week7 exception handling
PPT
Week7 exception handling
PPT
Week7 exception handling
PPTX
Computer programing 111 lecture 2
PPTX
Cp 111 lecture 2
PPTX
Production Debugging at Code Camp Philly
PDF
Building of systems of automatic C/C++ code logging
DOC
Software Bugs A Software Architect Point Of View
PPT
Intro To AOP
PPTX
Python Unit II.pptx
PPT
exceptionvdffhhhccvvvv-handling-in-java.ppt
PPT
Itp 120 Chapt 18 2009 Exceptions & Assertions
PPTX
Week 4 - 5 Debugging Code and Analyzing Logic Errors.pptx
Exception handling
S D D Program Development Tools
lecs101.pdfgggggggggggggggggggddddddddddddb
Exceptions overview
exception-handling-in-java.ppt
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
Week7 exception handling
Week7 exception handling
Week7 exception handling
Computer programing 111 lecture 2
Cp 111 lecture 2
Production Debugging at Code Camp Philly
Building of systems of automatic C/C++ code logging
Software Bugs A Software Architect Point Of View
Intro To AOP
Python Unit II.pptx
exceptionvdffhhhccvvvv-handling-in-java.ppt
Itp 120 Chapt 18 2009 Exceptions & Assertions
Week 4 - 5 Debugging Code and Analyzing Logic Errors.pptx
Ad

Recently uploaded (20)

PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Queuing formulas to evaluate throughputs and servers
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
OOP with Java - Java Introduction (Basics)
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
PDF
Monitoring Global Terrestrial Surface Water Height using Remote Sensing - ARS...
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT
Chapter 6 Design in software Engineeing.ppt
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PDF
composite construction of structures.pdf
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Queuing formulas to evaluate throughputs and servers
Arduino robotics embedded978-1-4302-3184-4.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
OOP with Java - Java Introduction (Basics)
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
Monitoring Global Terrestrial Surface Water Height using Remote Sensing - ARS...
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Chapter 6 Design in software Engineeing.ppt
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
composite construction of structures.pdf
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
bas. eng. economics group 4 presentation 1.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Ad

Exception Handling in Python topic .pptx

  • 2. What is an Exception? In Python, an exception is an event that occurs during the execution of a program that disrupts the normal flow of the program's instructions. Why exceptions occur in programs? Code errors: A software engineer may make a mistake, such as using a null resource or passing the wrong parameter to a function Invalid user input: The user may input something that is invalid or unacceptable Device failure: A system failure can occur, which is a run-time error that cannot be handled programmatically Arithmetic errors: A program may make a mistake in a mathematical calculation or operation
  • 3. Importance of Exception Handling Exception handling is important because it allows programs to handle errors that occur during execution. It's especially important in the following situations: •Handling errors Exception handling allows programs to distinguish between normal and erroneous return values. This is especially important in geospatial programming, where issues like missing files or corrupt data can occur. Separating error-handling code Exception handling separates the details of what to do when something unexpected happens from the main logic of a program Handling concurrency In multi-threaded applications, exceptions thrown by one thread can propagate to other threads, causing the application to crash. Exception handling can prevent this from happening.
  • 5. The TRY block lets you test a block of code for error. The EXCEPT block lets you handle the error. Syntax: try: # Code that may cause an exception except ExceptionType: # Code to handle the exception The try-except Block try: x = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!") The try-except Block
  • 6. try: even_numbers = [2,4,6,8] print(even_numbers[5]) except ZeroDivisionError: print("Denominator cannot be 0.") except IndexError: print("Index Out of Bound.") # Output: Index Out of Bound Multiple except Block try: num = int(input("Enter a number: ")) num % 2 == 0 except: print("Not an even number!") else: print("its a even number") The else Clause