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

PPTX
Python Exception Handling
Megha V
 
PPTX
File handling in Python
Megha V
 
PDF
IT Strategy Framework
Vishal Sharma
 
PPTX
Python Functions
Mohammed Sikander
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPTX
Introduction to SQL
Ehsan Hamzei
 
PDF
Python revision tour i
Mr. Vikram Singh Slathia
 
PDF
Python revision tour II
Mr. Vikram Singh Slathia
 
Python Exception Handling
Megha V
 
File handling in Python
Megha V
 
IT Strategy Framework
Vishal Sharma
 
Python Functions
Mohammed Sikander
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Introduction to SQL
Ehsan Hamzei
 
Python revision tour i
Mr. Vikram Singh Slathia
 
Python revision tour II
Mr. Vikram Singh Slathia
 

What's hot (20)

PPTX
Data types in python
RaginiJain21
 
PPTX
Functions in python slide share
Devashish Kumar
 
PDF
Strings in python
Prabhakaran V M
 
PDF
Operators in python
Prabhakaran V M
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
Functions in python
colorsof
 
PDF
Python set
Mohammed Sikander
 
PPTX
Constructor ppt
Vinod Kumar
 
PDF
sparse matrix in data structure
MAHALAKSHMI P
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PPTX
Recursion
Abdur Rehman
 
PDF
Datatypes in python
eShikshak
 
PDF
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
PPTX
Data types in C
Tarun Sharma
 
PPTX
Exception handling c++
Jayant Dalvi
 
PDF
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
PPTX
class and objects
Payel Guria
 
PPS
Java Exception handling
kamal kotecha
 
PDF
Set methods in python
deepalishinkar1
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
Data types in python
RaginiJain21
 
Functions in python slide share
Devashish Kumar
 
Strings in python
Prabhakaran V M
 
Operators in python
Prabhakaran V M
 
Python Modules
Nitin Reddy Katkam
 
Functions in python
colorsof
 
Python set
Mohammed Sikander
 
Constructor ppt
Vinod Kumar
 
sparse matrix in data structure
MAHALAKSHMI P
 
Modules and packages in python
TMARAGATHAM
 
Recursion
Abdur Rehman
 
Datatypes in python
eShikshak
 
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Data types in C
Tarun Sharma
 
Exception handling c++
Jayant Dalvi
 
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
class and objects
Payel Guria
 
Java Exception handling
kamal kotecha
 
Set methods in python
deepalishinkar1
 
Python: Modules and Packages
Damian T. Gordon
 
Ad

Similar to Python exception handling (20)

PPTX
exception handling.pptx
AbinayaC11
 
PPTX
Exception handling.pptx
NISHASOMSCS113
 
PPTX
Exceptions overview
Bharath K
 
PDF
Exception handling basic
TharuniDiddekunta
 
PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
poongothai11
 
PPTX
Exception Handling,finally,catch,throw,throws,try.pptx
ArunPatrick2
 
PPT
Exceptionhandling
DrHemlathadhevi
 
PPTX
Exception handling in java
Kavitha713564
 
PPT
Exception Handling using Python Libraries
mmvrm
 
PPT
Exception handling in python and how to handle it
s6901412
 
PPTX
4185_Exception-Handling-in-Java object oriented programming
vamsiKrishnasai3
 
PPTX
EXCEPTION HANDLING IN PYTHON For students .py.pptx
MihirBhardwaj3
 
PPT
Firoze_Errors_Exceptions in python__.ppt
urstrulysurya
 
PPT
Firoze_Errors_Exceptions in python__.ppt
urstrulysurya
 
PPT
Unit 5 Java
arnold 7490
 
PPTX
Exception Handling.pptx
primevideos176
 
PPT
Exception Handling on 22nd March 2022.ppt
Raja Ram Dutta
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PDF
summarizer16fev16_Exceptions
Filipe Morais Jorge
 
PPTX
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
mcaajiet25
 
exception handling.pptx
AbinayaC11
 
Exception handling.pptx
NISHASOMSCS113
 
Exceptions overview
Bharath K
 
Exception handling basic
TharuniDiddekunta
 
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
poongothai11
 
Exception Handling,finally,catch,throw,throws,try.pptx
ArunPatrick2
 
Exceptionhandling
DrHemlathadhevi
 
Exception handling in java
Kavitha713564
 
Exception Handling using Python Libraries
mmvrm
 
Exception handling in python and how to handle it
s6901412
 
4185_Exception-Handling-in-Java object oriented programming
vamsiKrishnasai3
 
EXCEPTION HANDLING IN PYTHON For students .py.pptx
MihirBhardwaj3
 
Firoze_Errors_Exceptions in python__.ppt
urstrulysurya
 
Firoze_Errors_Exceptions in python__.ppt
urstrulysurya
 
Unit 5 Java
arnold 7490
 
Exception Handling.pptx
primevideos176
 
Exception Handling on 22nd March 2022.ppt
Raja Ram Dutta
 
Exception Handling in JAVA
SURIT DATTA
 
summarizer16fev16_Exceptions
Filipe Morais Jorge
 
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
mcaajiet25
 
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 dictionary
Mohammed Sikander
 
PDF
Python tuple
Mohammed Sikander
 
PDF
Python strings
Mohammed Sikander
 
PDF
Python list
Mohammed Sikander
 
PDF
Python Flow Control
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
 
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 dictionary
Mohammed Sikander
 
Python tuple
Mohammed Sikander
 
Python strings
Mohammed Sikander
 
Python list
Mohammed Sikander
 
Python Flow Control
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
 

Recently uploaded (20)

PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
Landforms and landscapes data surprise preview
jpinnuck
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 

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