SlideShare a Scribd company logo
EXCEPTION HANDLING
IN PYTHON
By:- Animesh
Naugai
So…. what is EXCEPTION HANDLING ?

Exception Handling is the process of handling the expected or
unexpected exceptions.

You can even raise a user defined error message.

There are several Exception Handling specific keywords in
python.

For Exception Handling you don’t need to import any internal
or external library or module.
Using Exception Handling we can avoid unexpected termination during
code execution process and most importantly avoiding those scary looking
error messages.
For example :-

below there are two image of same error(ZeroDivisionError, which
happens when we try to perform division by zero) (a) is with out
exception handling whereas (b) is with exception handling.

We can see the difference between two (b) much more friendly whereas
(a) is nerdy.
Why do we need Exception
Handling ?
(a (b
What is an Exception ?

Exceptions are a means of breaking out of the normal flow
of control of a code block in order to handle errors or other
exceptional Conditions.

An exception is *raised* at the point where the error is
detected; it may be *handled* by the surrounding code
block or by any code block that directly or indirectly invoked
the code block where the error occurred.
The hierarchical representation of EXCEPTIONS is below :-
**Note**
Whenever an
exception is caught by
the handler it will
return exception class
instance according to
the **except** clause.
Which means other
than just the exception
class name it will also
contain the stack
traceback, except
when the exception is
"SystemExit".
How do we handle exceptions ?
In python we some special keywords to implement the exception
handling, which are as follows :-

Try : *try* statement is the start of exception handling block.

Except : *except* statement is the end of exception handling block.

Raise : *raise* statement allows user to raise exception.

Finally : *finally* statement is used as clean up section for safe
termination.
Lets look at each of them with some code………….
Basic exception handling code looks like
this ..
Now if we try to run it, it will ask us to enter a number but u can enter
an alphabet too in which case it will cause the error while converting
alpha character to integer which will look like this ..
So what actually happened here is that it asked a number and we
entered non numeric value to it which caused a runtime error or an
exception.
In previous example it was pretty obvious about the type of error but
what happen if there are multiple statements inside ‘try – except’
block.
Now if we run it again we will
need to enter a value and If we
enter an non-numeric value
result will be “ValueError”
whereas on entering a numeric
value doesn’t give anything
except if we enter ‘0’ which will
return “DivisionByZeroError”
just as shown in the image.
**Note** Some() function is
same as code above.
There is a add on to this try-except block which is capability of using else
and finally keywords.
Finally keyword is used as cleanup section it runs regardless of whether
‘try-except’ block has caught any error or not, except when any syntax error
or forced termination occurs.
Here is how it looks like in code ..
The else statement executes if no exception has been caught by ‘try-except’
block.
The use of the else statement with ‘try-except’ block is better than adding
additional code to the try clause because it avoids accidentally catching an
exception that wasn’t raised by the code being protected by the ‘try-except’
block.
In the right most image we some
stack traceback report telling error
with line number, here during
handling of ‘ValueError’ another
error occurred which wasn’t handled
by ‘try-except’.

More Related Content

Similar to Python Exceptions Powerpoint Presentation (20)

PPTX
Python Exception Handling
Megha V
 
PPTX
Exception Handling.pptx
Pavan326406
 
PPTX
ACP - Week - 9.pptx
funnyvideosbysam
 
PPTX
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
 
PDF
lecs101.pdfgggggggggggggggggggddddddddddddb
MrProfEsOr1
 
PPT
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
 
PPT
Exception Handling on 22nd March 2022.ppt
Raja Ram Dutta
 
PPT
Exception
Navaneethan Naveen
 
PDF
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
 
PDF
Exception handling in python
Lifna C.S
 
PPTX
Chapter 13 exceptional handling
Praveen M Jigajinni
 
PPTX
Python Exception Handling (Python Course)
IGustiNyomanAntonSur
 
PDF
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
PPTX
Exception handling.pptx
NISHASOMSCS113
 
PPTX
Python Lecture 7
Inzamam Baig
 
PDF
Python exception handling
Mohammed Sikander
 
DOCX
Exception handlingpdf
gandra jeeshitha
 
PDF
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
PPT
Exception handling in python and how to handle it
s6901412
 
Python Exception Handling
Megha V
 
Exception Handling.pptx
Pavan326406
 
ACP - Week - 9.pptx
funnyvideosbysam
 
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
 
lecs101.pdfgggggggggggggggggggddddddddddddb
MrProfEsOr1
 
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
 
Exception Handling on 22nd March 2022.ppt
Raja Ram Dutta
 
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
 
Exception handling in python
Lifna C.S
 
Chapter 13 exceptional handling
Praveen M Jigajinni
 
Python Exception Handling (Python Course)
IGustiNyomanAntonSur
 
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Exception handling.pptx
NISHASOMSCS113
 
Python Lecture 7
Inzamam Baig
 
Python exception handling
Mohammed Sikander
 
Exception handlingpdf
gandra jeeshitha
 
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
Exception handling in python and how to handle it
s6901412
 

Recently uploaded (20)

PPTX
BMC S6 M3 P1 building mATERIALS AND CONSTRUCTION.pptx
RizwanAlavi
 
PPTX
(2) Cell Wall Inhibitors_Cephalosporins others.pptx
mkurdi133
 
PPTX
英国硕士毕业证伦敦城市大学学位证书City毕业完成信办理
Taqyea
 
PDF
Florida Gulf Coast University (FGCU) Diploma
bogusdiploma
 
PPTX
Cisco SDWAN presentation for Headquarters
dayoo0186
 
PPTX
feminist gnsudnshxujenduxhsixisjxuu.pptx
rowvinafujimoto
 
PDF
Dynamic Visuals for NJ Commercial Spaces
Yantram Animation Studio Corporation
 
DOCX
Ai vehicle traffic signal detector research proposal.docx
DavidNameza
 
PDF
CRAC- Adobe Photoshop CC 2016 (32 64Bit) Crack
utfefguu
 
PDF
safiyayahaya-portfoliohighres-171228115344.pdf
architectsdreamdesig1
 
PDF
Favorite Looks Menswear Spring Summer 2026
Ana Andjelic
 
PDF
JeevanCare Transforming Healthcare Interaction through UX/UI Design
YellowSlice1
 
PPTX
Types of post tensioning methods (2).pptx
RizwanAlavi
 
PDF
Presentation of design made by power point
habibikuw002
 
PPTX
search engine optimization123244556.pptx
revathi148366
 
PPTX
sCREW cONVEYOR AUGER DAF SLUDGE SYSTEM TO
viksurs
 
PPTX
4. PMES PORTFOLIO_BROWN DESIGN_T1-T3_A4.pptx
GynnelNicanor1
 
PPT
Origin of the solar system acording .ppt
ReignLachica
 
PPTX
619813902-Fun-friday-Identify-Bollywood-movie-from-dialogues-deliver-the-dial...
in4withme
 
PPTX
SAMPLE FILE OF-PPT-FINAL-ORAL-DEFENSE.pptx
Yvez2
 
BMC S6 M3 P1 building mATERIALS AND CONSTRUCTION.pptx
RizwanAlavi
 
(2) Cell Wall Inhibitors_Cephalosporins others.pptx
mkurdi133
 
英国硕士毕业证伦敦城市大学学位证书City毕业完成信办理
Taqyea
 
Florida Gulf Coast University (FGCU) Diploma
bogusdiploma
 
Cisco SDWAN presentation for Headquarters
dayoo0186
 
feminist gnsudnshxujenduxhsixisjxuu.pptx
rowvinafujimoto
 
Dynamic Visuals for NJ Commercial Spaces
Yantram Animation Studio Corporation
 
Ai vehicle traffic signal detector research proposal.docx
DavidNameza
 
CRAC- Adobe Photoshop CC 2016 (32 64Bit) Crack
utfefguu
 
safiyayahaya-portfoliohighres-171228115344.pdf
architectsdreamdesig1
 
Favorite Looks Menswear Spring Summer 2026
Ana Andjelic
 
JeevanCare Transforming Healthcare Interaction through UX/UI Design
YellowSlice1
 
Types of post tensioning methods (2).pptx
RizwanAlavi
 
Presentation of design made by power point
habibikuw002
 
search engine optimization123244556.pptx
revathi148366
 
sCREW cONVEYOR AUGER DAF SLUDGE SYSTEM TO
viksurs
 
4. PMES PORTFOLIO_BROWN DESIGN_T1-T3_A4.pptx
GynnelNicanor1
 
Origin of the solar system acording .ppt
ReignLachica
 
619813902-Fun-friday-Identify-Bollywood-movie-from-dialogues-deliver-the-dial...
in4withme
 
SAMPLE FILE OF-PPT-FINAL-ORAL-DEFENSE.pptx
Yvez2
 
Ad

Python Exceptions Powerpoint Presentation

  • 2. So…. what is EXCEPTION HANDLING ?  Exception Handling is the process of handling the expected or unexpected exceptions.  You can even raise a user defined error message.  There are several Exception Handling specific keywords in python.  For Exception Handling you don’t need to import any internal or external library or module.
  • 3. Using Exception Handling we can avoid unexpected termination during code execution process and most importantly avoiding those scary looking error messages. For example :-  below there are two image of same error(ZeroDivisionError, which happens when we try to perform division by zero) (a) is with out exception handling whereas (b) is with exception handling.  We can see the difference between two (b) much more friendly whereas (a) is nerdy. Why do we need Exception Handling ? (a (b
  • 4. What is an Exception ?  Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional Conditions.  An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred.
  • 5. The hierarchical representation of EXCEPTIONS is below :- **Note** Whenever an exception is caught by the handler it will return exception class instance according to the **except** clause. Which means other than just the exception class name it will also contain the stack traceback, except when the exception is "SystemExit".
  • 6. How do we handle exceptions ? In python we some special keywords to implement the exception handling, which are as follows :-  Try : *try* statement is the start of exception handling block.  Except : *except* statement is the end of exception handling block.  Raise : *raise* statement allows user to raise exception.  Finally : *finally* statement is used as clean up section for safe termination. Lets look at each of them with some code………….
  • 7. Basic exception handling code looks like this .. Now if we try to run it, it will ask us to enter a number but u can enter an alphabet too in which case it will cause the error while converting alpha character to integer which will look like this .. So what actually happened here is that it asked a number and we entered non numeric value to it which caused a runtime error or an exception.
  • 8. In previous example it was pretty obvious about the type of error but what happen if there are multiple statements inside ‘try – except’ block. Now if we run it again we will need to enter a value and If we enter an non-numeric value result will be “ValueError” whereas on entering a numeric value doesn’t give anything except if we enter ‘0’ which will return “DivisionByZeroError” just as shown in the image. **Note** Some() function is same as code above.
  • 9. There is a add on to this try-except block which is capability of using else and finally keywords. Finally keyword is used as cleanup section it runs regardless of whether ‘try-except’ block has caught any error or not, except when any syntax error or forced termination occurs. Here is how it looks like in code ..
  • 10. The else statement executes if no exception has been caught by ‘try-except’ block. The use of the else statement with ‘try-except’ block is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the ‘try-except’ block. In the right most image we some stack traceback report telling error with line number, here during handling of ‘ValueError’ another error occurred which wasn’t handled by ‘try-except’.