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

PDF
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
PDF
CSV Files-1.pdf
PPTX
Exception handling in Java
PPTX
Top down and botttom up Parsing
PPTX
exception handling
PPTX
Presentation1
PPTX
Chapter 13 exceptional handling
PPTX
File handling in c language
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
CSV Files-1.pdf
Exception handling in Java
Top down and botttom up Parsing
exception handling
Presentation1
Chapter 13 exceptional handling
File handling in c language

What's hot (20)

PPTX
Chapter 17 Tuples
PDF
2 problem solving and programming workbook by inqilab patel
PPTX
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
PDF
Python iteration
PPTX
EXCEPTION HANDLING IN PYTHON For students .py.pptx
PDF
Python If Else | If Else Statement In Python | Edureka
PDF
Python programming : Exceptions
PPTX
Python Exception Handling
PPTX
Iostream in c++
PPTX
If statements in c programming
PPTX
Operators and expressions
PPTX
Intro to Python Programming Language
PPTX
Unit I - Evaluation of expression
PPTX
Recursion
PDF
PDF
Chapter Functions for grade 12 computer Science
PDF
Function in Python
PPTX
Python Data-Types
PDF
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
PPSX
Complete C++ programming Language Course
Chapter 17 Tuples
2 problem solving and programming workbook by inqilab patel
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
Python iteration
EXCEPTION HANDLING IN PYTHON For students .py.pptx
Python If Else | If Else Statement In Python | Edureka
Python programming : Exceptions
Python Exception Handling
Iostream in c++
If statements in c programming
Operators and expressions
Intro to Python Programming Language
Unit I - Evaluation of expression
Recursion
Chapter Functions for grade 12 computer Science
Function in Python
Python Data-Types
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
Complete C++ programming Language Course
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
PPTX
Python_Exception_Handling_Presentation.pptx
PPTX
Exception handling in python
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_Exception_Handling_Presentation.pptx
Exception handling in python
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
Complications of Minimal Access Surgery at WLH
PDF
Classroom Observation Tools for Teachers
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Updated Idioms and Phrasal Verbs in English subject
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
Cell Structure & Organelles in detailed.
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Complications of Minimal Access Surgery at WLH
Classroom Observation Tools for Teachers
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
Orientation - ARALprogram of Deped to the Parents.pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
History, Philosophy and sociology of education (1).pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
LDMMIA Reiki Yoga Finals Review Spring Summer
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Updated Idioms and Phrasal Verbs in English subject
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
What if we spent less time fighting change, and more time building what’s rig...
Cell Structure & Organelles in detailed.
UNIT III MENTAL HEALTH NURSING ASSESSMENT

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.