SlideShare a Scribd company logo
2
Most read
4
Most read
22
Most read
Chapter 1
Exception handling
Ne
w
syllabu
s
2024-25
Exception
Handling
As a student of computer science, you know that many
times while executing the program we are getting
some errors.
Errors are problems in a program due to
which will stop the program from execution.
These errors are categorized as follows:
Syntax Errors : occures when the program is not written
correctly as per the format required
Example : x =+ 2
Semantic Errors : occur when the statement has no
meaning in the program.
Example: a=5 #Sattement 1
b=7 #Statement 2
a+b=res #Statement 3
Exceptions
An exception is a program event that occurs during
program execution and disrupts the flow of a program.
When a python program cannot Handle with a situation, it
raises an exception.
An exception is a Python object that represents an error.
Some common Python Build-in exceptions are as follows:
1) SyntaxError: This exception is raised when the interpreter
encounters a syntax error the code, such as a misspelled
keyword, a missing colon, or an unbalanced parenthesis.
2) TypeError: This exception is raised when an operation or
function is applied to an object of the wrong type, such as
adding a string to an integer.
3) NameError: This exception is raised when a variable or
function name is not found in the current scope.
4) IndexError: This exception is raised when an index is out of
range for a list, tuple, or other sequence types.
5) KeyError: This exception is raised when a key is not found in a
dictionary.
6) ValueError: This exception is raised when a function or method
is called with an invalid argument or input, such as trying to
convert a string to an integer when the string does not represent
a valid integer.
7) AttributeError: This exception is raised when an attribute or
method is not found on an object, such as trying to access a non-
existent attribute of a class instance.
8) IOError: This exception is raised when an I/O operation,
such as reading or writing a file, fails due to an input/output
error.
9) ZeroDivisionError: This exception is raised when an attempt is
made to divide a number by zero.
10) ImportError: This exception is raised when an import
statement fails to find or load a module.
Raising Exceptions
Exceptions are raised when the program is
syntactically correct, but the code results in an error.
This error does not stop the execution of the
program, however, it changes the normal flow of
the program.
Example: a = 10 / 0
print(a)
User-defined exceptions
The exception created by the programmer according to
the requirement of the program is called user-defined
exceptions.
The user defined-exception can be created using two
methods:
1) raise statement
2) assert statement
raise statement
It is used to throw an exception. The syntax is as follows:
raise exception-name[(optional argument)]
Note: The optional argument is a string passed to the
exception, that displays the message.
Example:
assert statement
An assert statement is used to check a condition in the
program code. If the result after evaluation is false, then
the exception is raised.
The syntax for the assert statement is:
If this expression is false, an AssertionError exception is
raised which can be handled like any other exception.
Example:
What is Exception Handling in Python
The process of catching and preventing errors when they
occurred is called exception handling. It is a mechanism
to overrule the exceptions using some blocks.
It is the concept of error handling when something goes
wrong, tracking the error, and calling the handling code.
The following terms are used for exception handling:
handling exceptionsusing
try-except-finally blocks
try:
# Some Code....which may have runtime error
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)
try:
k = 9//0 # raises divide by zero
exception.
print(k)
# handles zerodivision
exception except
ZeroDivisionError:
print("Can't divide by zero")
finally:
# this block is always
executed
# regardless of exception
generation. print('This is always
executed')
Lets see some example’s for better
understanding of Try and Except block
Use of
try and except
Use of
try-except-finally
Use of
try-except-
else-finally
Process of Handling
Exception Step 1: The exception object is created by a
Python interpreter that contains information
related to the error such as type, file name,
and position where an error has occurred.
Step 2: The object is handed over to the
runtime system to find an appropriate code
to handle exceptions. This process is called
throwing an exception.
Step 3: The runtime system searches for a
block of code known as an exception handler
that handles the raised error. First, it searches
for the method by which the error has
occurred. If not found then it search method
from which this method is called. This
process continues till the exception handler is
found. When it found a handler it will be
executed. This process is known as catching.
Step 4: Finally the program gets terminated
Advantages of Exception Handling:
• Improved program reliability
• Simplified error handling
• Cleaner code
• Easier debugging
Disadvantages of Exception
Handling:
• Performance overhead
• Increased code complexity
• Possible security risks
Thank You
Any doubts ?
Assignment Questions
Q.1 What is exception handling explain with example.
Q.2 What are the advantages and Disadvantages of
Exception handling.
Q.3 Explain the use of try-except-else-finally blocks.
Q.4 Write a short note on assert and raise statment
Q.5 Explain the process of Handling Exception.
Practical Questions

More Related Content

PPTX
Libre Office Calc Lesson 1: Introduction to spreadsheets
PPTX
Python Control Structures.pptx
PPTX
Adobe Pagemaker 7.0
PPTX
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
PDF
Chapter Functions for grade 12 computer Science
PPTX
Operators in Python
PDF
Nim games
PPT
Basics of C programming
Libre Office Calc Lesson 1: Introduction to spreadsheets
Python Control Structures.pptx
Adobe Pagemaker 7.0
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
Chapter Functions for grade 12 computer Science
Operators in Python
Nim games
Basics of C programming

What's hot (20)

PPTX
Microsoft word
PPTX
Chapter 14 strings
PPTX
C and its errors
PPTX
Libre Office Writer Lesson 1
PPSX
Files in c++
PPS
PPTX
Python dictionary
PDF
Python lambda functions with filter, map & reduce function
PDF
Contact management system
PPTX
Python Exception Handling
PDF
Library management system
DOCX
Practical File of C Language
PPT
Mesics lecture 6 control statement = if -else if__else
PPT
Ms excel (page layout)
PPTX
Operators and expressions
PPTX
Class 10 Unit - 6 Digital Documentation.pptx
PPTX
Preprocessor directives in c language
PPT
How to manage the table of contents with one click (Microsoft Word 2007) - fo...
Microsoft word
Chapter 14 strings
C and its errors
Libre Office Writer Lesson 1
Files in c++
Python dictionary
Python lambda functions with filter, map & reduce function
Contact management system
Python Exception Handling
Library management system
Practical File of C Language
Mesics lecture 6 control statement = if -else if__else
Ms excel (page layout)
Operators and expressions
Class 10 Unit - 6 Digital Documentation.pptx
Preprocessor directives in c language
How to manage the table of contents with one click (Microsoft Word 2007) - fo...
Ad

Similar to Exception handling with python class 12.pptx (20)

PDF
Python Programming - X. Exception Handling and Assertions
PDF
lecs101.pdfgggggggggggggggggggddddddddddddb
PPTX
Exception Handling in python programming.pptx
PPTX
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
PPTX
Exception handling.pptxnn h
PPT
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
PPTX
Exception Handling in Python Programming.pptx
PPTX
Exception Handling in Python programming.pptx
DOCX
Exception handlingpdf
PPTX
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
PPTX
Exception handling in Python
PPTX
Python Exception Handling (Python Course)
PPTX
Exception Handling.pptx
PPTX
Python Exceptions Powerpoint Presentation
PPT
PDF
Exception handling in python
PPT
Exception Handling on 22nd March 2022.ppt
PDF
Python programming : Exceptions
PPTX
Python_Exception_Handling_Presentation.pptx
Python Programming - X. Exception Handling and Assertions
lecs101.pdfgggggggggggggggggggddddddddddddb
Exception Handling in python programming.pptx
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
Exception handling.pptxnn h
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
Exception Handling in Python Programming.pptx
Exception Handling in Python programming.pptx
Exception handlingpdf
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Exception handling in Python
Python Exception Handling (Python Course)
Exception Handling.pptx
Python Exceptions Powerpoint Presentation
Exception handling in python
Exception Handling on 22nd March 2022.ppt
Python programming : Exceptions
Python_Exception_Handling_Presentation.pptx
Ad

More from PreeTVithule1 (7)

PPTX
File handling for reference class 12.pptx
PPTX
01 file handling for class use class pptx
PPTX
Data Base Management 1 Database Management.pptx
PPT
network protocols7 class 12 computer .ppt
PPT
Network and network types6 class 12 computer.ppt
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PPTX
7-2-data-structures-ii-stacks-queues.pptx
File handling for reference class 12.pptx
01 file handling for class use class pptx
Data Base Management 1 Database Management.pptx
network protocols7 class 12 computer .ppt
Network and network types6 class 12 computer.ppt
CHAPTER 01 FUNCTION in python class 12th.pptx
7-2-data-structures-ii-stacks-queues.pptx

Recently uploaded (20)

PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
Onica Farming 24rsclub profitable farm business
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
From loneliness to social connection charting
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Cell Structure & Organelles in detailed.
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Open Quiz Monsoon Mind Game Final Set.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Open folder Downloads.pdf yes yes ges yes
Onica Farming 24rsclub profitable farm business
STATICS OF THE RIGID BODIES Hibbelers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
human mycosis Human fungal infections are called human mycosis..pptx
Week 4 Term 3 Study Techniques revisited.pptx
From loneliness to social connection charting
Insiders guide to clinical Medicine.pdf
Cell Structure & Organelles in detailed.
UPPER GASTRO INTESTINAL DISORDER.docx
O5-L3 Freight Transport Ops (International) V1.pdf
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Open Quiz Monsoon Mind Game Prelims.pptx
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)

Exception handling with python class 12.pptx

  • 2. Exception Handling As a student of computer science, you know that many times while executing the program we are getting some errors. Errors are problems in a program due to which will stop the program from execution. These errors are categorized as follows:
  • 3. Syntax Errors : occures when the program is not written correctly as per the format required Example : x =+ 2 Semantic Errors : occur when the statement has no meaning in the program. Example: a=5 #Sattement 1 b=7 #Statement 2 a+b=res #Statement 3
  • 4. Exceptions An exception is a program event that occurs during program execution and disrupts the flow of a program. When a python program cannot Handle with a situation, it raises an exception. An exception is a Python object that represents an error. Some common Python Build-in exceptions are as follows: 1) SyntaxError: This exception is raised when the interpreter encounters a syntax error the code, such as a misspelled keyword, a missing colon, or an unbalanced parenthesis. 2) TypeError: This exception is raised when an operation or function is applied to an object of the wrong type, such as adding a string to an integer.
  • 5. 3) NameError: This exception is raised when a variable or function name is not found in the current scope. 4) IndexError: This exception is raised when an index is out of range for a list, tuple, or other sequence types. 5) KeyError: This exception is raised when a key is not found in a dictionary. 6) ValueError: This exception is raised when a function or method is called with an invalid argument or input, such as trying to convert a string to an integer when the string does not represent a valid integer. 7) AttributeError: This exception is raised when an attribute or method is not found on an object, such as trying to access a non- existent attribute of a class instance.
  • 6. 8) IOError: This exception is raised when an I/O operation, such as reading or writing a file, fails due to an input/output error. 9) ZeroDivisionError: This exception is raised when an attempt is made to divide a number by zero. 10) ImportError: This exception is raised when an import statement fails to find or load a module.
  • 7. Raising Exceptions Exceptions are raised when the program is syntactically correct, but the code results in an error. This error does not stop the execution of the program, however, it changes the normal flow of the program. Example: a = 10 / 0 print(a)
  • 8. User-defined exceptions The exception created by the programmer according to the requirement of the program is called user-defined exceptions. The user defined-exception can be created using two methods: 1) raise statement 2) assert statement
  • 9. raise statement It is used to throw an exception. The syntax is as follows: raise exception-name[(optional argument)] Note: The optional argument is a string passed to the exception, that displays the message. Example:
  • 10. assert statement An assert statement is used to check a condition in the program code. If the result after evaluation is false, then the exception is raised. The syntax for the assert statement is: If this expression is false, an AssertionError exception is raised which can be handled like any other exception. Example:
  • 11. What is Exception Handling in Python The process of catching and preventing errors when they occurred is called exception handling. It is a mechanism to overrule the exceptions using some blocks. It is the concept of error handling when something goes wrong, tracking the error, and calling the handling code. The following terms are used for exception handling:
  • 12. handling exceptionsusing try-except-finally blocks try: # Some Code....which may have runtime error except: # optional block # Handling of exception (if required) else: # execute if no exception finally: # Some code .....(always executed)
  • 13. try: k = 9//0 # raises divide by zero exception. print(k) # handles zerodivision exception except ZeroDivisionError: print("Can't divide by zero") finally: # this block is always executed # regardless of exception generation. print('This is always executed')
  • 14. Lets see some example’s for better understanding of Try and Except block
  • 15. Use of try and except
  • 18. Process of Handling Exception Step 1: The exception object is created by a Python interpreter that contains information related to the error such as type, file name, and position where an error has occurred. Step 2: The object is handed over to the runtime system to find an appropriate code to handle exceptions. This process is called throwing an exception. Step 3: The runtime system searches for a block of code known as an exception handler that handles the raised error. First, it searches for the method by which the error has occurred. If not found then it search method from which this method is called. This process continues till the exception handler is found. When it found a handler it will be executed. This process is known as catching. Step 4: Finally the program gets terminated
  • 19. Advantages of Exception Handling: • Improved program reliability • Simplified error handling • Cleaner code • Easier debugging Disadvantages of Exception Handling: • Performance overhead • Increased code complexity • Possible security risks
  • 21. Assignment Questions Q.1 What is exception handling explain with example. Q.2 What are the advantages and Disadvantages of Exception handling. Q.3 Explain the use of try-except-else-finally blocks. Q.4 Write a short note on assert and raise statment Q.5 Explain the process of Handling Exception.