ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
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

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

Py-ºÝºÝߣs-9.ppt
Py-ºÝºÝߣs-9.pptPy-ºÝºÝߣs-9.ppt
Py-ºÝºÝߣs-9.ppt
wulanpermatasari27
Ìý
Exception handling3.pdf
Exception handling3.pdfException handling3.pdf
Exception handling3.pdf
Brokeass1
Ìý
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
Praveen M Jigajinni
Ìý
Introduction to Python Prog. - Lecture 3
Introduction to Python Prog. - Lecture 3Introduction to Python Prog. - Lecture 3
Introduction to Python Prog. - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
Ìý
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
sarthakgithub
Ìý
Python Session - 6
Python Session - 6Python Session - 6
Python Session - 6
AnirudhaGaikwad4
Ìý
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
Rakesh Madugula
Ìý
lecs101.pdfgggggggggggggggggggddddddddddddb
lecs101.pdfgggggggggggggggggggddddddddddddblecs101.pdfgggggggggggggggggggddddddddddddb
lecs101.pdfgggggggggggggggggggddddddddddddb
MrProfEsOr1
Ìý
Exception handling
Exception handlingException handling
Exception handling
Karthik Sekar
Ìý
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
VGaneshKarthikeyan
Ìý
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
Ajit Mali
Ìý
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptxErrorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
xaybhagfsus
Ìý
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
Types of ExceptionsfsfsfsdfsdfdsfsfsdfsdTypes of Exceptionsfsfsfsdfsdfdsfsfsdfsd
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
ssuserce9e9d
Ìý
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
amiralicomsats3
Ìý
unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
Ìý
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
nehakumari0xf
Ìý
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
kashyapneha2809
Ìý
Exception
ExceptionException
Exception
abhay singh
Ìý
Event handling
Event handlingEvent handling
Event handling
Mohamed Essam
Ìý
Java_Prog_-_UNIT-IV_Part_A_Modinnfy.pptx
Java_Prog_-_UNIT-IV_Part_A_Modinnfy.pptxJava_Prog_-_UNIT-IV_Part_A_Modinnfy.pptx
Java_Prog_-_UNIT-IV_Part_A_Modinnfy.pptx
likithbas
Ìý
Exception handling3.pdf
Exception handling3.pdfException handling3.pdf
Exception handling3.pdf
Brokeass1
Ìý
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
Praveen M Jigajinni
Ìý
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
sarthakgithub
Ìý
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
Rakesh Madugula
Ìý
lecs101.pdfgggggggggggggggggggddddddddddddb
lecs101.pdfgggggggggggggggggggddddddddddddblecs101.pdfgggggggggggggggggggddddddddddddb
lecs101.pdfgggggggggggggggggggddddddddddddb
MrProfEsOr1
Ìý
Exception handling
Exception handlingException handling
Exception handling
Karthik Sekar
Ìý
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
Ajit Mali
Ìý
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptxErrorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
xaybhagfsus
Ìý
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
Types of ExceptionsfsfsfsdfsdfdsfsfsdfsdTypes of Exceptionsfsfsfsdfsdfdsfsfsdfsd
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
ssuserce9e9d
Ìý
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
amiralicomsats3
Ìý
unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
Ìý
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
nehakumari0xf
Ìý
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
kashyapneha2809
Ìý
Event handling
Event handlingEvent handling
Event handling
Mohamed Essam
Ìý
Java_Prog_-_UNIT-IV_Part_A_Modinnfy.pptx
Java_Prog_-_UNIT-IV_Part_A_Modinnfy.pptxJava_Prog_-_UNIT-IV_Part_A_Modinnfy.pptx
Java_Prog_-_UNIT-IV_Part_A_Modinnfy.pptx
likithbas
Ìý

More from PreeTVithule1 (7)

File handling for reference class 12.pptx
File handling for reference class 12.pptxFile handling for reference class 12.pptx
File handling for reference class 12.pptx
PreeTVithule1
Ìý
01 file handling for class use class pptx
01 file handling for class use class pptx01 file handling for class use class pptx
01 file handling for class use class pptx
PreeTVithule1
Ìý
Data Base Management 1 Database Management.pptx
Data Base Management 1 Database Management.pptxData Base Management 1 Database Management.pptx
Data Base Management 1 Database Management.pptx
PreeTVithule1
Ìý
network protocols7 class 12 computer .ppt
network protocols7 class 12 computer .pptnetwork protocols7 class 12 computer .ppt
network protocols7 class 12 computer .ppt
PreeTVithule1
Ìý
Network and network types6 class 12 computer.ppt
Network and network types6 class 12 computer.pptNetwork and network types6 class 12 computer.ppt
Network and network types6 class 12 computer.ppt
PreeTVithule1
Ìý
CHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptxCHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
Ìý
7-2-data-structures-ii-stacks-queues.pptx
7-2-data-structures-ii-stacks-queues.pptx7-2-data-structures-ii-stacks-queues.pptx
7-2-data-structures-ii-stacks-queues.pptx
PreeTVithule1
Ìý
File handling for reference class 12.pptx
File handling for reference class 12.pptxFile handling for reference class 12.pptx
File handling for reference class 12.pptx
PreeTVithule1
Ìý
01 file handling for class use class pptx
01 file handling for class use class pptx01 file handling for class use class pptx
01 file handling for class use class pptx
PreeTVithule1
Ìý
Data Base Management 1 Database Management.pptx
Data Base Management 1 Database Management.pptxData Base Management 1 Database Management.pptx
Data Base Management 1 Database Management.pptx
PreeTVithule1
Ìý
network protocols7 class 12 computer .ppt
network protocols7 class 12 computer .pptnetwork protocols7 class 12 computer .ppt
network protocols7 class 12 computer .ppt
PreeTVithule1
Ìý
Network and network types6 class 12 computer.ppt
Network and network types6 class 12 computer.pptNetwork and network types6 class 12 computer.ppt
Network and network types6 class 12 computer.ppt
PreeTVithule1
Ìý
CHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptxCHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
Ìý
7-2-data-structures-ii-stacks-queues.pptx
7-2-data-structures-ii-stacks-queues.pptx7-2-data-structures-ii-stacks-queues.pptx
7-2-data-structures-ii-stacks-queues.pptx
PreeTVithule1
Ìý

Recently uploaded (20)

How to Setup Company Data in Odoo 17 Accounting App
How to Setup Company Data in Odoo 17 Accounting AppHow to Setup Company Data in Odoo 17 Accounting App
How to Setup Company Data in Odoo 17 Accounting App
Celine George
Ìý
Design approaches and ethical challenges in Artificial Intelligence tools for...
Design approaches and ethical challenges in Artificial Intelligence tools for...Design approaches and ethical challenges in Artificial Intelligence tools for...
Design approaches and ethical challenges in Artificial Intelligence tools for...
Yannis
Ìý
ANTIVIRAL agent by Mrs. Manjushri Dabhade
ANTIVIRAL agent by Mrs. Manjushri DabhadeANTIVIRAL agent by Mrs. Manjushri Dabhade
ANTIVIRAL agent by Mrs. Manjushri Dabhade
Dabhade madam Dabhade
Ìý
Week 6 - EDL 290F - No Drop Ride (2025).pdf
Week 6 - EDL 290F - No Drop Ride (2025).pdfWeek 6 - EDL 290F - No Drop Ride (2025).pdf
Week 6 - EDL 290F - No Drop Ride (2025).pdf
Liz Walsh-Trevino
Ìý
MIPLM subject matter expert Dr Robert Klinski
MIPLM subject matter expert Dr Robert KlinskiMIPLM subject matter expert Dr Robert Klinski
MIPLM subject matter expert Dr Robert Klinski
MIPLM
Ìý
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VIAnti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Samruddhi Khonde
Ìý
Sulfonamides by Mrs. Manjushri P. Dabhade
Sulfonamides by Mrs. Manjushri P. DabhadeSulfonamides by Mrs. Manjushri P. Dabhade
Sulfonamides by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Ìý
The basics of sentences session 9pptx.pptx
The basics of sentences session 9pptx.pptxThe basics of sentences session 9pptx.pptx
The basics of sentences session 9pptx.pptx
heathfieldcps1
Ìý
Quizzitch Cup_Sports Quiz 2025_Prelims.pptx
Quizzitch Cup_Sports Quiz 2025_Prelims.pptxQuizzitch Cup_Sports Quiz 2025_Prelims.pptx
Quizzitch Cup_Sports Quiz 2025_Prelims.pptx
Anand Kumar
Ìý
MIPLM subject matter expert Nicos Raftis
MIPLM subject matter expert Nicos RaftisMIPLM subject matter expert Nicos Raftis
MIPLM subject matter expert Nicos Raftis
MIPLM
Ìý
CLEFT LIP AND PALATE: NURSING MANAGEMENT.pptx
CLEFT LIP AND PALATE: NURSING MANAGEMENT.pptxCLEFT LIP AND PALATE: NURSING MANAGEMENT.pptx
CLEFT LIP AND PALATE: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
Ìý
Antifungal agents by Mrs. Manjushri Dabhade
Antifungal agents by Mrs. Manjushri DabhadeAntifungal agents by Mrs. Manjushri Dabhade
Antifungal agents by Mrs. Manjushri Dabhade
Dabhade madam Dabhade
Ìý
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
Amlan Sarkar
Ìý
How to Install Odoo 18 with Pycharm - Odoo 18 ºÝºÝߣs
How to Install Odoo 18 with Pycharm - Odoo 18 ºÝºÝߣsHow to Install Odoo 18 with Pycharm - Odoo 18 ºÝºÝߣs
How to Install Odoo 18 with Pycharm - Odoo 18 ºÝºÝߣs
Celine George
Ìý
Berry_Kanisha_BAS_PB1_202503 (2) (2).pdf
Berry_Kanisha_BAS_PB1_202503 (2) (2).pdfBerry_Kanisha_BAS_PB1_202503 (2) (2).pdf
Berry_Kanisha_BAS_PB1_202503 (2) (2).pdf
KanishaBerry
Ìý
Studying and Notetaking: Some Suggestions
Studying and Notetaking: Some SuggestionsStudying and Notetaking: Some Suggestions
Studying and Notetaking: Some Suggestions
Damian T. Gordon
Ìý
MIPLM subject matter expert Sascha Kamhuber
MIPLM subject matter expert Sascha KamhuberMIPLM subject matter expert Sascha Kamhuber
MIPLM subject matter expert Sascha Kamhuber
MIPLM
Ìý
Introduction to Systematic Reviews - Prof Ejaz Khan
Introduction to Systematic Reviews - Prof Ejaz KhanIntroduction to Systematic Reviews - Prof Ejaz Khan
Introduction to Systematic Reviews - Prof Ejaz Khan
Systematic Reviews Network (SRN)
Ìý
NURSING PROCESS AND ITS STEPS .pptx
NURSING PROCESS AND ITS STEPS                 .pptxNURSING PROCESS AND ITS STEPS                 .pptx
NURSING PROCESS AND ITS STEPS .pptx
PoojaSen20
Ìý
How to Setup Company Data in Odoo 17 Accounting App
How to Setup Company Data in Odoo 17 Accounting AppHow to Setup Company Data in Odoo 17 Accounting App
How to Setup Company Data in Odoo 17 Accounting App
Celine George
Ìý
Design approaches and ethical challenges in Artificial Intelligence tools for...
Design approaches and ethical challenges in Artificial Intelligence tools for...Design approaches and ethical challenges in Artificial Intelligence tools for...
Design approaches and ethical challenges in Artificial Intelligence tools for...
Yannis
Ìý
ANTIVIRAL agent by Mrs. Manjushri Dabhade
ANTIVIRAL agent by Mrs. Manjushri DabhadeANTIVIRAL agent by Mrs. Manjushri Dabhade
ANTIVIRAL agent by Mrs. Manjushri Dabhade
Dabhade madam Dabhade
Ìý
Week 6 - EDL 290F - No Drop Ride (2025).pdf
Week 6 - EDL 290F - No Drop Ride (2025).pdfWeek 6 - EDL 290F - No Drop Ride (2025).pdf
Week 6 - EDL 290F - No Drop Ride (2025).pdf
Liz Walsh-Trevino
Ìý
MIPLM subject matter expert Dr Robert Klinski
MIPLM subject matter expert Dr Robert KlinskiMIPLM subject matter expert Dr Robert Klinski
MIPLM subject matter expert Dr Robert Klinski
MIPLM
Ìý
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VIAnti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Samruddhi Khonde
Ìý
Sulfonamides by Mrs. Manjushri P. Dabhade
Sulfonamides by Mrs. Manjushri P. DabhadeSulfonamides by Mrs. Manjushri P. Dabhade
Sulfonamides by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Ìý
The basics of sentences session 9pptx.pptx
The basics of sentences session 9pptx.pptxThe basics of sentences session 9pptx.pptx
The basics of sentences session 9pptx.pptx
heathfieldcps1
Ìý
Quizzitch Cup_Sports Quiz 2025_Prelims.pptx
Quizzitch Cup_Sports Quiz 2025_Prelims.pptxQuizzitch Cup_Sports Quiz 2025_Prelims.pptx
Quizzitch Cup_Sports Quiz 2025_Prelims.pptx
Anand Kumar
Ìý
MIPLM subject matter expert Nicos Raftis
MIPLM subject matter expert Nicos RaftisMIPLM subject matter expert Nicos Raftis
MIPLM subject matter expert Nicos Raftis
MIPLM
Ìý
CLEFT LIP AND PALATE: NURSING MANAGEMENT.pptx
CLEFT LIP AND PALATE: NURSING MANAGEMENT.pptxCLEFT LIP AND PALATE: NURSING MANAGEMENT.pptx
CLEFT LIP AND PALATE: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
Ìý
Antifungal agents by Mrs. Manjushri Dabhade
Antifungal agents by Mrs. Manjushri DabhadeAntifungal agents by Mrs. Manjushri Dabhade
Antifungal agents by Mrs. Manjushri Dabhade
Dabhade madam Dabhade
Ìý
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
Amlan Sarkar
Ìý
How to Install Odoo 18 with Pycharm - Odoo 18 ºÝºÝߣs
How to Install Odoo 18 with Pycharm - Odoo 18 ºÝºÝߣsHow to Install Odoo 18 with Pycharm - Odoo 18 ºÝºÝߣs
How to Install Odoo 18 with Pycharm - Odoo 18 ºÝºÝߣs
Celine George
Ìý
Berry_Kanisha_BAS_PB1_202503 (2) (2).pdf
Berry_Kanisha_BAS_PB1_202503 (2) (2).pdfBerry_Kanisha_BAS_PB1_202503 (2) (2).pdf
Berry_Kanisha_BAS_PB1_202503 (2) (2).pdf
KanishaBerry
Ìý
Studying and Notetaking: Some Suggestions
Studying and Notetaking: Some SuggestionsStudying and Notetaking: Some Suggestions
Studying and Notetaking: Some Suggestions
Damian T. Gordon
Ìý
MIPLM subject matter expert Sascha Kamhuber
MIPLM subject matter expert Sascha KamhuberMIPLM subject matter expert Sascha Kamhuber
MIPLM subject matter expert Sascha Kamhuber
MIPLM
Ìý
NURSING PROCESS AND ITS STEPS .pptx
NURSING PROCESS AND ITS STEPS                 .pptxNURSING PROCESS AND ITS STEPS                 .pptx
NURSING PROCESS AND ITS STEPS .pptx
PoojaSen20
Ìý

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.