際際滷

際際滷Share a Scribd company logo
As Per CBSE
Syllabus
2023-24
Computer Science
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Types of Errors:
Types of errors :
(i) Compile-time errors. These are the errors
resulting out of violation of programming
languages grammar rules. All syntax errors
are reported during compilation.
(ii) Run-time errors. The errors that occur during
runtime because of unexpected situations. Such
errors are handled through exception handling
routines of Python.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Syntax Error Examples:
These are the errors resulting out of
violation of programming
languages grammar rules.
Note: All syntax errors are reported
during compilation.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
What is an Exception :
Exceptions are unexpected events or errors that occur during the execution of a program, such as a
division by zero or accessing an invalid memory location. These events can lead to program termination or
incorrect results.
It is an exceptional event that occurs during runtime and causes normal program flow to be disrupted.
Some common examples of Exceptions are :
 Divide by zero errors
 Accessing the elements of an array beyond its range
 Invalid input m Hard disk crash
 Opening a non-existent file
 Heap memory exhausted
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Exception (Examples):
Note: Observe that there is nothing
wrong with the program syntax, it is
only when we try to divide an integer
with zero , an exception is generated.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Exception (Examples):
Note: Only When we are trying to
access a list element with an non
existing index an exception is
generated.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Exception (Examples):
Note: Only When we are trying to
convert a string to integer the
Exception generated.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
What is Exception Handling?
Exception handling in Python is a mechanism used to handle runtime errors that occur
during the execution of a Python program
Exception handling allows a program to gracefully handle such exceptions and recover
from errors by taking corrective actions instead of terminating abruptly. In Python,
exception handling is implemented using a try-except block
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Exception Handling using. try and except Block:
The try and except block in Python is a way to handle exceptions or errors that may occur during code
execution. This mechanism prevents the program from crashing by allowing it to continue running even if an
error is encountered.
In a try block, you write the code that might raise an exception. If an exception occurs, the code execution
jumps to the corresponding except block, where you can handle the error or take alternative actions.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Exception (Examples):
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Example:
Write a program to ensure that an integer is entered as input and in case any other value is entered, it displays a
message  Not a valid integer
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
General Built-in Python Exceptions:
Exception Name Description
EOFError
Raised when one of the built-in functions (input( )) hits an end-of-file condition (EOF) without reading any data. (NOTE. the file.read( ) and
file.readline( ) methods return an empty string when they hit EOF.)
IO Error
Raised when an I/O operation (such as a print statement, the built-in open( ) function or a method of a file object) fails for an I/O-related reason, e.g.,
file not found or disk full.
NameError
Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the
name that could not be found.
IndexError
Raised when a sequence subscript is out of range, e.g., from a list of length 4 if you try to read a value of index like 8 or E8 etc. (Slice indices are silently
truncated to fall in the allowed range ; if an index is not a plain integer, TypeError is raised.)
ImportError Raised when an import statement fails to find the module definition or when a from ... import fails to find a name that is to be imported.
TypeError
Raised when an operation or function is applied to an object of inappropriate type, e.g., if you try to compute a square-root of a string value. The
associated value is a string giving details about the type mismatch.
ValueError
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described
by a more precise exception such as IndexError.
ZeroDivisionError Raised when the second argument of a division or modulo operation is zero.
OverflowError Raised when the result of an arithmetic operation is too large to be represented.
KeyError Raised when a mapping (dictionary) key is not found in the set of existing keys
ImportError Raised when the module given with import statement is not found.
KeyboardInterrupt Raised when keys Esc, Del or Ctrl+C is pressed during program execution and normal program flow gets disturbed.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Second Argument of the Exception Block:
We can also provide a second argument (optional) for the except block, which gives a reference to the
exception object.
try:
#code
Except <Exception Name> as <Argument>:
#code for error handling here
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Handling Multiple Errors
Handling multiple exceptions in Python allows a single try-except block to handle different types of exceptions using
multiple except blocks. This allows a program to handle various types of errors that may occur during runtime and take
corrective measures accordingly.
In a try-except block, each except block is associated with a specific exception type, and the block containing the code to
handle that exception is executed if the corresponding exception occurs in the try block. By handling multiple
exceptions, programmers can write more robust and less error-prone code.
Syntax:
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Example:
Program to handle multiple exceptions:
Note (Execution Order):
The <try suite> is executed first ; if, during the
course of executing the <try suite>, an
exception is raised that is not handled
otherwise, and the <except suite> is executed,
with <name> bound to the exception, if found ;
if no matching except suite is found then
unnamed except suite is executed.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
finally Block :
The finally block is a part of the try-except block in Python that contains the code that is executed
regardless of whether an exception is raised or not. The syntax of the try-except-finally block is as follows:
the try block contains the code that may raise an exception. If an exception occurs, the control is
transferred to the corresponding except block, which contains the code to handle the exception. The finally
block contains the code that is executed after the try-except blocks, regardless of whether an exception
occurred or not.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Example :
Program using finally block
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Example :
What is the order of execution?
In this example, if the user enters an invalid
input or attempts to divide by zero, the
corresponding except block handles the
exception and prints an error message to the
user. If no exception occurs, the else block is
executed and prints the result. Finally, the finally
block is executed and prints a message to
indicate the completion of the program
execution
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Practice Programs Example :
1. Write a Python program that takes two numbers as input from the user and calculates the quotient of the two
numbers. Handle the exceptions that may occur during the program execution, such as invalid input or division
by zero.
2. Write a Python program that reads a file and displays its contents on the screen. Handle the exceptions that
may occur during the program execution, such as the file not found error or file reading error.
3. Write a Python program that takes a list of integers as input from the user and calculates the average of the
numbers. Handle the exceptions that may occur during the program execution, such as invalid input or division
by zero.
4. Write a Python program that reads a CSV file and displays its contents on the screen. Handle the exceptions
that may occur during the program execution, such as the file not found error or file reading error.
5. Write a Python program that takes a string as input from the user and converts it to an integer. Handle the
exceptions that may occur during the program execution, such as invalid input or string conversion error.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE
Practice Programs Example :
6. Write a Python program that takes a list of numbers as input from the user and finds the maximum and
minimum numbers in the list. Handle the exceptions that may occur during the program execution, such as
invalid input or empty list error.
7. Write a Python program that reads a file and writes its contents to another file. Handle the exceptions that
may occur during the program execution, such as the file not found error or file reading/writing error.
8. Write a Python program that takes two strings as input from the user and concatenates them. Handle the
exceptions that may occur during the program execution, such as invalid input or string concatenation error.
9. Write a Python program that reads a text file and counts the number of words in it. Handle the exceptions that
may occur during the program execution, such as the file not found error or file reading error.
10. Write a Python program that takes a string as input from the user and reverses it. Handle the exceptions that
may occur during the program execution, such as invalid input or string reversal error.
Visit www.kvcoders.in more regular updates
K V C o d e r s
LETS CRACK CBSE COMUPTER SCIENCE

More Related Content

Similar to Exception-Handling Exception-HandlingFpptx.pdf (20)

Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.net
suraj pandey
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.net
suraj pandey
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
Event handling
Event handlingEvent handling
Event handling
Mohamed Essam
Event handling
Event handlingEvent handling
Event handling
Mohamed Essam
C Language Presentation.pptx
C Language Presentation.pptxC Language Presentation.pptx
C Language Presentation.pptx
PradeepKumar206701
C Language Presentation.pptx
C Language Presentation.pptxC Language Presentation.pptx
C Language Presentation.pptx
PradeepKumar206701
java programm for beginners (basic) level
java programm for beginners (basic) leveljava programm for beginners (basic) level
java programm for beginners (basic) level
SurendarKesavan4
java programm for beginners (basic) level
java programm for beginners (basic) leveljava programm for beginners (basic) level
java programm for beginners (basic) level
SurendarKesavan4
LECT 29- EXCEPTION HANDLING.pptx b.
LECT 29- EXCEPTION HANDLING.pptx      b.LECT 29- EXCEPTION HANDLING.pptx      b.
LECT 29- EXCEPTION HANDLING.pptx b.
gjhp9927
LECT 29- EXCEPTION HANDLING.pptx b.
LECT 29- EXCEPTION HANDLING.pptx      b.LECT 29- EXCEPTION HANDLING.pptx      b.
LECT 29- EXCEPTION HANDLING.pptx b.
gjhp9927
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx   RUNTIME ERRORS HANDLINGEXCEPTIONS-PYTHON.pptx   RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx   RUNTIME ERRORS HANDLINGEXCEPTIONS-PYTHON.pptx   RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
S D D Program Development Tools
S D D  Program  Development  ToolsS D D  Program  Development  Tools
S D D Program Development Tools
gavhays
S D D Program Development Tools
S D D  Program  Development  ToolsS D D  Program  Development  Tools
S D D Program Development Tools
gavhays
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
Cherimay Batallones
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
Cherimay Batallones
Test Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTest Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code Analyzer
Terry Yin
Test Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTest Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code Analyzer
Terry Yin
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.net
suraj pandey
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.net
suraj pandey
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
C Language Presentation.pptx
C Language Presentation.pptxC Language Presentation.pptx
C Language Presentation.pptx
PradeepKumar206701
C Language Presentation.pptx
C Language Presentation.pptxC Language Presentation.pptx
C Language Presentation.pptx
PradeepKumar206701
java programm for beginners (basic) level
java programm for beginners (basic) leveljava programm for beginners (basic) level
java programm for beginners (basic) level
SurendarKesavan4
java programm for beginners (basic) level
java programm for beginners (basic) leveljava programm for beginners (basic) level
java programm for beginners (basic) level
SurendarKesavan4
LECT 29- EXCEPTION HANDLING.pptx b.
LECT 29- EXCEPTION HANDLING.pptx      b.LECT 29- EXCEPTION HANDLING.pptx      b.
LECT 29- EXCEPTION HANDLING.pptx b.
gjhp9927
LECT 29- EXCEPTION HANDLING.pptx b.
LECT 29- EXCEPTION HANDLING.pptx      b.LECT 29- EXCEPTION HANDLING.pptx      b.
LECT 29- EXCEPTION HANDLING.pptx b.
gjhp9927
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx   RUNTIME ERRORS HANDLINGEXCEPTIONS-PYTHON.pptx   RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx   RUNTIME ERRORS HANDLINGEXCEPTIONS-PYTHON.pptx   RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
S D D Program Development Tools
S D D  Program  Development  ToolsS D D  Program  Development  Tools
S D D Program Development Tools
gavhays
S D D Program Development Tools
S D D  Program  Development  ToolsS D D  Program  Development  Tools
S D D Program Development Tools
gavhays
Test Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTest Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code Analyzer
Terry Yin
Test Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTest Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code Analyzer
Terry Yin

Recently uploaded (20)

Unit 3: Combustion in Spark Ignition Engines
Unit 3: Combustion in Spark Ignition EnginesUnit 3: Combustion in Spark Ignition Engines
Unit 3: Combustion in Spark Ignition Engines
NileshKumbhar21
U.S. Department of Education certification
U.S. Department of Education certificationU.S. Department of Education certification
U.S. Department of Education certification
Mebane Rash
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
home
MIPLM subject matter expert Daniel Holzner
MIPLM subject matter expert Daniel HolznerMIPLM subject matter expert Daniel Holzner
MIPLM subject matter expert Daniel Holzner
MIPLM
MIPLM subject matter expert Sascha Kamhuber
MIPLM subject matter expert Sascha KamhuberMIPLM subject matter expert Sascha Kamhuber
MIPLM subject matter expert Sascha Kamhuber
MIPLM
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
Different Facets of Knowledge on different View.pptx
Different Facets of Knowledge on different View.pptxDifferent Facets of Knowledge on different View.pptx
Different Facets of Knowledge on different View.pptx
NrapendraVirSingh
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
 Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ... Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
coreylewis960
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
heathfieldcps1
Early 20th Century Modern Art: Movements and Artists
Early 20th Century Modern Art: Movements and ArtistsEarly 20th Century Modern Art: Movements and Artists
Early 20th Century Modern Art: Movements and Artists
Damian T. Gordon
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
General Quiz at Maharaja Agrasen College | Amlan Sarkar | Prelims with Answer...
General Quiz at Maharaja Agrasen College | Amlan Sarkar | Prelims with Answer...General Quiz at Maharaja Agrasen College | Amlan Sarkar | Prelims with Answer...
General Quiz at Maharaja Agrasen College | Amlan Sarkar | Prelims with Answer...
Amlan Sarkar
EDL 290F Week 5 - Facing Headwinds and Hairpin Turns (2025).pdf
EDL 290F Week 5  - Facing Headwinds and Hairpin Turns (2025).pdfEDL 290F Week 5  - Facing Headwinds and Hairpin Turns (2025).pdf
EDL 290F Week 5 - Facing Headwinds and Hairpin Turns (2025).pdf
Liz Walsh-Trevino
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT PPT.pptx
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT PPT.pptxANORECTAL MALFORMATIONS: NURSING MANAGEMENT PPT.pptx
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT PPT.pptx
PRADEEP ABOTHU
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
compiler design BCS613C question bank 2022 scheme
compiler design BCS613C question bank 2022 schemecompiler design BCS613C question bank 2022 scheme
compiler design BCS613C question bank 2022 scheme
Suvarna Hiremath
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Sue Beckingham
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
Unit1 Inroduction to Internal Combustion Engines
Unit1  Inroduction to Internal Combustion EnginesUnit1  Inroduction to Internal Combustion Engines
Unit1 Inroduction to Internal Combustion Engines
NileshKumbhar21
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
Unit 3: Combustion in Spark Ignition Engines
Unit 3: Combustion in Spark Ignition EnginesUnit 3: Combustion in Spark Ignition Engines
Unit 3: Combustion in Spark Ignition Engines
NileshKumbhar21
U.S. Department of Education certification
U.S. Department of Education certificationU.S. Department of Education certification
U.S. Department of Education certification
Mebane Rash
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
home
MIPLM subject matter expert Daniel Holzner
MIPLM subject matter expert Daniel HolznerMIPLM subject matter expert Daniel Holzner
MIPLM subject matter expert Daniel Holzner
MIPLM
MIPLM subject matter expert Sascha Kamhuber
MIPLM subject matter expert Sascha KamhuberMIPLM subject matter expert Sascha Kamhuber
MIPLM subject matter expert Sascha Kamhuber
MIPLM
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
Different Facets of Knowledge on different View.pptx
Different Facets of Knowledge on different View.pptxDifferent Facets of Knowledge on different View.pptx
Different Facets of Knowledge on different View.pptx
NrapendraVirSingh
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
 Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ... Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
coreylewis960
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
heathfieldcps1
Early 20th Century Modern Art: Movements and Artists
Early 20th Century Modern Art: Movements and ArtistsEarly 20th Century Modern Art: Movements and Artists
Early 20th Century Modern Art: Movements and Artists
Damian T. Gordon
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
General Quiz at Maharaja Agrasen College | Amlan Sarkar | Prelims with Answer...
General Quiz at Maharaja Agrasen College | Amlan Sarkar | Prelims with Answer...General Quiz at Maharaja Agrasen College | Amlan Sarkar | Prelims with Answer...
General Quiz at Maharaja Agrasen College | Amlan Sarkar | Prelims with Answer...
Amlan Sarkar
EDL 290F Week 5 - Facing Headwinds and Hairpin Turns (2025).pdf
EDL 290F Week 5  - Facing Headwinds and Hairpin Turns (2025).pdfEDL 290F Week 5  - Facing Headwinds and Hairpin Turns (2025).pdf
EDL 290F Week 5 - Facing Headwinds and Hairpin Turns (2025).pdf
Liz Walsh-Trevino
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT PPT.pptx
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT PPT.pptxANORECTAL MALFORMATIONS: NURSING MANAGEMENT PPT.pptx
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT PPT.pptx
PRADEEP ABOTHU
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
compiler design BCS613C question bank 2022 scheme
compiler design BCS613C question bank 2022 schemecompiler design BCS613C question bank 2022 scheme
compiler design BCS613C question bank 2022 scheme
Suvarna Hiremath
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Sue Beckingham
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
Unit1 Inroduction to Internal Combustion Engines
Unit1  Inroduction to Internal Combustion EnginesUnit1  Inroduction to Internal Combustion Engines
Unit1 Inroduction to Internal Combustion Engines
NileshKumbhar21
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

Exception-Handling Exception-HandlingFpptx.pdf

  • 1. As Per CBSE Syllabus 2023-24 Computer Science Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 2. Types of Errors: Types of errors : (i) Compile-time errors. These are the errors resulting out of violation of programming languages grammar rules. All syntax errors are reported during compilation. (ii) Run-time errors. The errors that occur during runtime because of unexpected situations. Such errors are handled through exception handling routines of Python. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 3. Syntax Error Examples: These are the errors resulting out of violation of programming languages grammar rules. Note: All syntax errors are reported during compilation. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 4. What is an Exception : Exceptions are unexpected events or errors that occur during the execution of a program, such as a division by zero or accessing an invalid memory location. These events can lead to program termination or incorrect results. It is an exceptional event that occurs during runtime and causes normal program flow to be disrupted. Some common examples of Exceptions are : Divide by zero errors Accessing the elements of an array beyond its range Invalid input m Hard disk crash Opening a non-existent file Heap memory exhausted Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 5. Exception (Examples): Note: Observe that there is nothing wrong with the program syntax, it is only when we try to divide an integer with zero , an exception is generated. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 6. Exception (Examples): Note: Only When we are trying to access a list element with an non existing index an exception is generated. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 7. Exception (Examples): Note: Only When we are trying to convert a string to integer the Exception generated. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 8. What is Exception Handling? Exception handling in Python is a mechanism used to handle runtime errors that occur during the execution of a Python program Exception handling allows a program to gracefully handle such exceptions and recover from errors by taking corrective actions instead of terminating abruptly. In Python, exception handling is implemented using a try-except block Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 9. Exception Handling using. try and except Block: The try and except block in Python is a way to handle exceptions or errors that may occur during code execution. This mechanism prevents the program from crashing by allowing it to continue running even if an error is encountered. In a try block, you write the code that might raise an exception. If an exception occurs, the code execution jumps to the corresponding except block, where you can handle the error or take alternative actions. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 10. Exception (Examples): Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 11. Example: Write a program to ensure that an integer is entered as input and in case any other value is entered, it displays a message Not a valid integer Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 12. General Built-in Python Exceptions: Exception Name Description EOFError Raised when one of the built-in functions (input( )) hits an end-of-file condition (EOF) without reading any data. (NOTE. the file.read( ) and file.readline( ) methods return an empty string when they hit EOF.) IO Error Raised when an I/O operation (such as a print statement, the built-in open( ) function or a method of a file object) fails for an I/O-related reason, e.g., file not found or disk full. NameError Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found. IndexError Raised when a sequence subscript is out of range, e.g., from a list of length 4 if you try to read a value of index like 8 or E8 etc. (Slice indices are silently truncated to fall in the allowed range ; if an index is not a plain integer, TypeError is raised.) ImportError Raised when an import statement fails to find the module definition or when a from ... import fails to find a name that is to be imported. TypeError Raised when an operation or function is applied to an object of inappropriate type, e.g., if you try to compute a square-root of a string value. The associated value is a string giving details about the type mismatch. ValueError Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError. ZeroDivisionError Raised when the second argument of a division or modulo operation is zero. OverflowError Raised when the result of an arithmetic operation is too large to be represented. KeyError Raised when a mapping (dictionary) key is not found in the set of existing keys ImportError Raised when the module given with import statement is not found. KeyboardInterrupt Raised when keys Esc, Del or Ctrl+C is pressed during program execution and normal program flow gets disturbed. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 13. Second Argument of the Exception Block: We can also provide a second argument (optional) for the except block, which gives a reference to the exception object. try: #code Except <Exception Name> as <Argument>: #code for error handling here Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 14. Handling Multiple Errors Handling multiple exceptions in Python allows a single try-except block to handle different types of exceptions using multiple except blocks. This allows a program to handle various types of errors that may occur during runtime and take corrective measures accordingly. In a try-except block, each except block is associated with a specific exception type, and the block containing the code to handle that exception is executed if the corresponding exception occurs in the try block. By handling multiple exceptions, programmers can write more robust and less error-prone code. Syntax: Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 15. Example: Program to handle multiple exceptions: Note (Execution Order): The <try suite> is executed first ; if, during the course of executing the <try suite>, an exception is raised that is not handled otherwise, and the <except suite> is executed, with <name> bound to the exception, if found ; if no matching except suite is found then unnamed except suite is executed. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 16. finally Block : The finally block is a part of the try-except block in Python that contains the code that is executed regardless of whether an exception is raised or not. The syntax of the try-except-finally block is as follows: the try block contains the code that may raise an exception. If an exception occurs, the control is transferred to the corresponding except block, which contains the code to handle the exception. The finally block contains the code that is executed after the try-except blocks, regardless of whether an exception occurred or not. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 17. Example : Program using finally block Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 18. Example : What is the order of execution? In this example, if the user enters an invalid input or attempts to divide by zero, the corresponding except block handles the exception and prints an error message to the user. If no exception occurs, the else block is executed and prints the result. Finally, the finally block is executed and prints a message to indicate the completion of the program execution Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 19. Practice Programs Example : 1. Write a Python program that takes two numbers as input from the user and calculates the quotient of the two numbers. Handle the exceptions that may occur during the program execution, such as invalid input or division by zero. 2. Write a Python program that reads a file and displays its contents on the screen. Handle the exceptions that may occur during the program execution, such as the file not found error or file reading error. 3. Write a Python program that takes a list of integers as input from the user and calculates the average of the numbers. Handle the exceptions that may occur during the program execution, such as invalid input or division by zero. 4. Write a Python program that reads a CSV file and displays its contents on the screen. Handle the exceptions that may occur during the program execution, such as the file not found error or file reading error. 5. Write a Python program that takes a string as input from the user and converts it to an integer. Handle the exceptions that may occur during the program execution, such as invalid input or string conversion error. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE
  • 20. Practice Programs Example : 6. Write a Python program that takes a list of numbers as input from the user and finds the maximum and minimum numbers in the list. Handle the exceptions that may occur during the program execution, such as invalid input or empty list error. 7. Write a Python program that reads a file and writes its contents to another file. Handle the exceptions that may occur during the program execution, such as the file not found error or file reading/writing error. 8. Write a Python program that takes two strings as input from the user and concatenates them. Handle the exceptions that may occur during the program execution, such as invalid input or string concatenation error. 9. Write a Python program that reads a text file and counts the number of words in it. Handle the exceptions that may occur during the program execution, such as the file not found error or file reading error. 10. Write a Python program that takes a string as input from the user and reverses it. Handle the exceptions that may occur during the program execution, such as invalid input or string reversal error. Visit www.kvcoders.in more regular updates K V C o d e r s LETS CRACK CBSE COMUPTER SCIENCE