SlideShare a Scribd company logo
3
Most read
5
Most read
6
Most read
Exception Handling in Python
Mohammed Sikander
Python
Exceptions
Exception Handling
Try and Except
Nested try Block
Handling Multiple Exceptions in single Except Block
Raising Exception
Finally Block
User Defined Exceptions
Exception
 When writing a program, we, more often than not,
will encounter errors.
 Error caused by not following the proper structure
(syntax) of the language is called syntax error or
parsing error
 Errors can also occur at runtime and these are called
exceptions.
 They occur, for example, when a file we try to open
does not exist (FileNotFoundError), dividing a
number by zero (ZeroDivisionError)
 Whenever these type of runtime error occur, Python
creates an exception object. If not handled properly, it
prints a traceback to that error along with some
details about why that error occurred.
Python   exception handling
Exception Handling
 To handle exceptions, and to call code when
an exception occurs, we can use a
try/except statement.
 The try block contains code that might
throw an exception.
 If that exception occurs, the code in the try
block stops being executed, and the code in
the except block is executed.
 If no error occurs, the code in the except
block doesn't execute.
Python   exception handling
Nested Try Block
Python   exception handling
 A try statement can have multiple
different except blocks to handle
different exceptions.
Python   exception handling
Python   exception handling
 Multiple exceptions can also be put into a
single except block using parentheses, to
have the except block handle all of them.
Python   exception handling
Python   exception handling
Raising Exceptions
Python   exception handling
Raising Exception from Except Block
finally
 To ensure some code runs no matter
what errors occur, you can use a finally
statement.
 The finally statement is placed at the
bottom of a try/except statement.
 Code within a finally statement always
runs after execution of the code in the
try, and possibly in the except, blocks.
Python   exception handling
Python   exception handling
 Code in a finally statement even runs if
an uncaught exception occurs in one of
the preceding blocks.
Raising Exception
 Raising exception is similar to throwing
exception in C++/Java.
 You can raise exceptions by using
the raise statement
User Defined Exception

More Related Content

What's hot (20)

ODP
Python Modules
Nitin Reddy Katkam
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PPTX
File handling in Python
Megha V
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PDF
Python tuple
Mohammed Sikander
 
PPTX
Python dictionary
Mohammed Sikander
 
PDF
Python set
Mohammed Sikander
 
PPTX
Chapter 08 data file handling
Praveen M Jigajinni
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPTX
python conditional statement.pptx
Dolchandra
 
PPTX
Functions in python
colorsof
 
PDF
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
PPTX
Data Structures in Python
Devashish Kumar
 
PDF
Python Flow Control
Mohammed Sikander
 
PDF
C++ OOPS Concept
Boopathi K
 
PDF
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
PPTX
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
PDF
Python final ppt
Ripal Ranpara
 
PPT
structure and union
student
 
Python Modules
Nitin Reddy Katkam
 
Modules and packages in python
TMARAGATHAM
 
File handling in Python
Megha V
 
Python: Modules and Packages
Damian T. Gordon
 
Python tuple
Mohammed Sikander
 
Python dictionary
Mohammed Sikander
 
Python set
Mohammed Sikander
 
Chapter 08 data file handling
Praveen M Jigajinni
 
Regular expressions in Python
Sujith Kumar
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
python conditional statement.pptx
Dolchandra
 
Functions in python
colorsof
 
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
Data Structures in Python
Devashish Kumar
 
Python Flow Control
Mohammed Sikander
 
C++ OOPS Concept
Boopathi K
 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
Python final ppt
Ripal Ranpara
 
structure and union
student
 

Similar to Python exception handling (20)

PPTX
exception handling.pptx
AbinayaC11
 
PPTX
ACP - Week - 9.pptx
funnyvideosbysam
 
PPTX
Exception Handling.pptx
Pavan326406
 
PPTX
Python Exceptions Powerpoint Presentation
mitchellblack733
 
PPTX
Exception handling with python class 12.pptx
PreeTVithule1
 
PPTX
Exception handling.pptx
NISHASOMSCS113
 
PPTX
Exception handling.pptxnn h
sabarivelan111007
 
PPTX
Exception Handling in python programming.pptx
shririshsri
 
PPT
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
 
PPT
Exception Handling on 22nd March 2022.ppt
Raja Ram Dutta
 
PPT
Exception Handling using Python Libraries
mmvrm
 
PDF
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
PPT
Exception handling in python and how to handle it
s6901412
 
PDF
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
PPTX
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
 
PPTX
Exception Handling in Python Programming.pptx
vinayagrawal71
 
PDF
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
 
PPT
Exception
Navaneethan Naveen
 
exception handling.pptx
AbinayaC11
 
ACP - Week - 9.pptx
funnyvideosbysam
 
Exception Handling.pptx
Pavan326406
 
Python Exceptions Powerpoint Presentation
mitchellblack733
 
Exception handling with python class 12.pptx
PreeTVithule1
 
Exception handling.pptx
NISHASOMSCS113
 
Exception handling.pptxnn h
sabarivelan111007
 
Exception Handling in python programming.pptx
shririshsri
 
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
 
Exception Handling on 22nd March 2022.ppt
Raja Ram Dutta
 
Exception Handling using Python Libraries
mmvrm
 
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Exception handling in python and how to handle it
s6901412
 
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
 
Exception Handling in Python Programming.pptx
vinayagrawal71
 
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
 
Ad

More from Mohammed Sikander (20)

PPTX
Strings in C - covers string functions
Mohammed Sikander
 
PDF
Smart Pointers, Modern Memory Management Techniques
Mohammed Sikander
 
PDF
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
PDF
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
PDF
Operator Overloading in C++
Mohammed Sikander
 
PDF
Python_Regular Expression
Mohammed Sikander
 
PPTX
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
PDF
Modern_cpp_auto.pdf
Mohammed Sikander
 
PPTX
Python Functions
Mohammed Sikander
 
PDF
Python list
Mohammed Sikander
 
PDF
Introduction to Python
Mohammed Sikander
 
PPTX
Pointer basics
Mohammed Sikander
 
PPTX
Signal
Mohammed Sikander
 
PPTX
File management
Mohammed Sikander
 
PPT
Functions in C++
Mohammed Sikander
 
PPT
CPP Language Basics - Reference
Mohammed Sikander
 
PPTX
Java arrays
Mohammed Sikander
 
PPTX
Java strings
Mohammed Sikander
 
PPTX
Java notes 1 - operators control-flow
Mohammed Sikander
 
Strings in C - covers string functions
Mohammed Sikander
 
Smart Pointers, Modern Memory Management Techniques
Mohammed Sikander
 
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
Operator Overloading in C++
Mohammed Sikander
 
Python_Regular Expression
Mohammed Sikander
 
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
Modern_cpp_auto.pdf
Mohammed Sikander
 
Python Functions
Mohammed Sikander
 
Python list
Mohammed Sikander
 
Introduction to Python
Mohammed Sikander
 
Pointer basics
Mohammed Sikander
 
File management
Mohammed Sikander
 
Functions in C++
Mohammed Sikander
 
CPP Language Basics - Reference
Mohammed Sikander
 
Java arrays
Mohammed Sikander
 
Java strings
Mohammed Sikander
 
Java notes 1 - operators control-flow
Mohammed Sikander
 
Ad

Recently uploaded (20)

PPTX
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPT
Indian Contract Act 1872, Business Law #MBA #BBA #BCOM
priyasinghy107
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Difference between write and update in odoo 18
Celine George
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
infertility, types,causes, impact, and management
Ritu480198
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Introduction presentation of the patentbutler tool
MIPLM
 
Indian Contract Act 1872, Business Law #MBA #BBA #BCOM
priyasinghy107
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
Horarios de distribución de agua en julio
pegazohn1978
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 

Python exception handling

  • 1. Exception Handling in Python Mohammed Sikander
  • 2. Python Exceptions Exception Handling Try and Except Nested try Block Handling Multiple Exceptions in single Except Block Raising Exception Finally Block User Defined Exceptions
  • 3. Exception  When writing a program, we, more often than not, will encounter errors.  Error caused by not following the proper structure (syntax) of the language is called syntax error or parsing error  Errors can also occur at runtime and these are called exceptions.  They occur, for example, when a file we try to open does not exist (FileNotFoundError), dividing a number by zero (ZeroDivisionError)  Whenever these type of runtime error occur, Python creates an exception object. If not handled properly, it prints a traceback to that error along with some details about why that error occurred.
  • 5. Exception Handling  To handle exceptions, and to call code when an exception occurs, we can use a try/except statement.  The try block contains code that might throw an exception.  If that exception occurs, the code in the try block stops being executed, and the code in the except block is executed.  If no error occurs, the code in the except block doesn't execute.
  • 9.  A try statement can have multiple different except blocks to handle different exceptions.
  • 12.  Multiple exceptions can also be put into a single except block using parentheses, to have the except block handle all of them.
  • 17. Raising Exception from Except Block
  • 18. finally  To ensure some code runs no matter what errors occur, you can use a finally statement.  The finally statement is placed at the bottom of a try/except statement.  Code within a finally statement always runs after execution of the code in the try, and possibly in the except, blocks.
  • 21.  Code in a finally statement even runs if an uncaught exception occurs in one of the preceding blocks.
  • 22. Raising Exception  Raising exception is similar to throwing exception in C++/Java.  You can raise exceptions by using the raise statement