Copyright IntelliPaat, All rights reserved
Python
Exception Handling
Copyright IntelliPaat, All rights reserved
Agenda
Copyright IntelliPaat, All rights reserved
Agenda
01 What are Exceptions?
Copyright IntelliPaat, All rights reserved
Agenda
02 Examples of Exceptions in
Python
01 What are Exceptions?
Copyright IntelliPaat, All rights reserved
Agenda
02 Examples of Exceptions in
Python
01 What are Exceptions?
03 How to handle Exceptions in Python
Copyright IntelliPaat, All rights reserved
Agenda
02 Examples of Exceptions in
Python
01 What are Exceptions?
03 How to handle Exceptions in Python
04 Demo
Copyright Intellipaat. All rights reserved.
What are
Exceptions?
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Definition
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Definition In programming, an exception is defined as an event which stops or
interrupts the intended flow of a program’s execution
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Types of
Programming
Languages
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Types of
Programming
Languages
Compiler-based Languages
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Types of
Programming
Languages
Compiler-based Languages Interpreter-based Languages
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Exceptions
in
Compiler-based
Languages
Start
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Exceptions
in
Compiler-based
Languages
Start
Code Module 1
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Exceptions
in
Compiler-based
Languages
Start
Code Module 1
Code Module 2
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Exceptions
in
Compiler-based
Languages
Start
Code Module 1
Code Module 2
Code Module 3
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Exceptions
in
Compiler-based
Languages
Start
Code Module 1
Code Module 2
Code Module 3
End
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Exceptions
in
Interpreter-based
Languages
Start
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Exceptions
in
Interpreter-based
Languages
Start
Code Module 1
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Exceptions
in
Interpreter-based
Languages
Start
Code Module 1
Code Module 2
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Exceptions
in
Interpreter-based
Languages
Start
Code Module 1
Code Module 2
Code Module 3
Copyright IntelliPaat, All rights reserved
What are Exceptions?
Exceptions
in
Interpreter-based
Languages
Start
Code Module 1
Code Module 2
Code Module 3
End
Will Not Execute
Any Further
Copyright IntelliPaat, All rights reserved
What are Exceptions?
How to handle
Exceptions
in Interpreter-
based Languages
Start
Code Module 1
Code Module 2
Code Module 3
End
If Exception
Copyright Intellipaat. All rights reserved.
Examples of
Exceptions in Python
Copyright IntelliPaat, All rights reserved
Examples of Exceptions in Python
ZeroDivisionError
Zero Division Error exceptions happen when the second argument of
a modulo operation or a division operation is 0
A = 100 % 0
B = 65 / 0
Copyright IntelliPaat, All rights reserved
Examples of Exceptions in Python
NameError
Name Error occurs when a variable name is used without prior
definition
A = B + 3
# B is not defined
Copyright IntelliPaat, All rights reserved
Examples of Exceptions in Python
TypeError
Type Error occurs when there is a datatype mismatch
A = “hello” + 4
# The integer 4 can’t be added to a string
Copyright Intellipaat. All rights reserved.
How to Handle
Exceptions in Python
Copyright IntelliPaat, All rights reserved
How to handle Exceptions in Python
What is Exception
Handling?
Exception Handling is defined as the process of adding failsafe
measures into the code, to keep the program running, in the case of
an Exception
Copyright IntelliPaat, All rights reserved
How to handle Exceptions in Python
‘try’ block
The ‘try’ clause is used to define the block of code that you intend to
successfully execute, if no exceptions occur
try:
print(x)
Copyright IntelliPaat, All rights reserved
How to handle Exceptions in Python
‘except’ block
The ‘except’ clause is used to define the block of code that you
would want to execute if and when an exception is encountered in
the try block
try:
print(x)
except:
print(“Exception”)
Copyright IntelliPaat, All rights reserved
How to handle Exceptions in Python
‘else’ block
The ‘else’ clause is used to define the block of code that you would
want to execute optionally if NO errors are encountered in the try
block
try:
print(x)
except:
print(“Exception”)
else:
print(“No errors”)
Copyright IntelliPaat, All rights reserved
How to handle Exceptions in Python
‘finally’ block
The ‘finally’ clause is used to define the block of code that you would
want to execute optionally regardless of the presence of errors in the
try block
try:
print(x)
except:
print(“Exception”)
finally:
print(“Will always execute”)
Copyright Intellipaat. All rights reserved.
Demo
Copyright Intellipaat. All rights reserved.
India: +91-7847955955
US: 1-800-216-8930 (TOLL FREE)
support@intellipaat.com
24/7 Chat with Our Course Advisor
sales@intellipaat.com

Exception handling in python

  • 1.
    Copyright IntelliPaat, Allrights reserved Python Exception Handling
  • 2.
    Copyright IntelliPaat, Allrights reserved Agenda
  • 3.
    Copyright IntelliPaat, Allrights reserved Agenda 01 What are Exceptions?
  • 4.
    Copyright IntelliPaat, Allrights reserved Agenda 02 Examples of Exceptions in Python 01 What are Exceptions?
  • 5.
    Copyright IntelliPaat, Allrights reserved Agenda 02 Examples of Exceptions in Python 01 What are Exceptions? 03 How to handle Exceptions in Python
  • 6.
    Copyright IntelliPaat, Allrights reserved Agenda 02 Examples of Exceptions in Python 01 What are Exceptions? 03 How to handle Exceptions in Python 04 Demo
  • 7.
    Copyright Intellipaat. Allrights reserved. What are Exceptions?
  • 8.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Definition
  • 9.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Definition In programming, an exception is defined as an event which stops or interrupts the intended flow of a program’s execution
  • 10.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Types of Programming Languages
  • 11.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Types of Programming Languages Compiler-based Languages
  • 12.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Types of Programming Languages Compiler-based Languages Interpreter-based Languages
  • 13.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Exceptions in Compiler-based Languages Start
  • 14.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Exceptions in Compiler-based Languages Start Code Module 1
  • 15.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Exceptions in Compiler-based Languages Start Code Module 1 Code Module 2
  • 16.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Exceptions in Compiler-based Languages Start Code Module 1 Code Module 2 Code Module 3
  • 17.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Exceptions in Compiler-based Languages Start Code Module 1 Code Module 2 Code Module 3 End
  • 18.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Exceptions in Interpreter-based Languages Start
  • 19.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Exceptions in Interpreter-based Languages Start Code Module 1
  • 20.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Exceptions in Interpreter-based Languages Start Code Module 1 Code Module 2
  • 21.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Exceptions in Interpreter-based Languages Start Code Module 1 Code Module 2 Code Module 3
  • 22.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? Exceptions in Interpreter-based Languages Start Code Module 1 Code Module 2 Code Module 3 End Will Not Execute Any Further
  • 23.
    Copyright IntelliPaat, Allrights reserved What are Exceptions? How to handle Exceptions in Interpreter- based Languages Start Code Module 1 Code Module 2 Code Module 3 End If Exception
  • 24.
    Copyright Intellipaat. Allrights reserved. Examples of Exceptions in Python
  • 25.
    Copyright IntelliPaat, Allrights reserved Examples of Exceptions in Python ZeroDivisionError Zero Division Error exceptions happen when the second argument of a modulo operation or a division operation is 0 A = 100 % 0 B = 65 / 0
  • 26.
    Copyright IntelliPaat, Allrights reserved Examples of Exceptions in Python NameError Name Error occurs when a variable name is used without prior definition A = B + 3 # B is not defined
  • 27.
    Copyright IntelliPaat, Allrights reserved Examples of Exceptions in Python TypeError Type Error occurs when there is a datatype mismatch A = “hello” + 4 # The integer 4 can’t be added to a string
  • 28.
    Copyright Intellipaat. Allrights reserved. How to Handle Exceptions in Python
  • 29.
    Copyright IntelliPaat, Allrights reserved How to handle Exceptions in Python What is Exception Handling? Exception Handling is defined as the process of adding failsafe measures into the code, to keep the program running, in the case of an Exception
  • 30.
    Copyright IntelliPaat, Allrights reserved How to handle Exceptions in Python ‘try’ block The ‘try’ clause is used to define the block of code that you intend to successfully execute, if no exceptions occur try: print(x)
  • 31.
    Copyright IntelliPaat, Allrights reserved How to handle Exceptions in Python ‘except’ block The ‘except’ clause is used to define the block of code that you would want to execute if and when an exception is encountered in the try block try: print(x) except: print(“Exception”)
  • 32.
    Copyright IntelliPaat, Allrights reserved How to handle Exceptions in Python ‘else’ block The ‘else’ clause is used to define the block of code that you would want to execute optionally if NO errors are encountered in the try block try: print(x) except: print(“Exception”) else: print(“No errors”)
  • 33.
    Copyright IntelliPaat, Allrights reserved How to handle Exceptions in Python ‘finally’ block The ‘finally’ clause is used to define the block of code that you would want to execute optionally regardless of the presence of errors in the try block try: print(x) except: print(“Exception”) finally: print(“Will always execute”)
  • 34.
    Copyright Intellipaat. Allrights reserved. Demo
  • 35.
    Copyright Intellipaat. Allrights reserved. India: +91-7847955955 US: 1-800-216-8930 (TOLL FREE) support@intellipaat.com 24/7 Chat with Our Course Advisor sales@intellipaat.com

Editor's Notes

  • #2 Welcome back to the forth module of data science.