EXCEPTION HANDLING class 12th computer science.pptx
1.
NAME OF TEACHER:MRS SUMAN VERMA, PGT(CS)
DATE : July 24, 2024
CLASS AND SECTION / STREAM: XII A , Science Stream
SUBJECT: Computer Science
DETAILS OF TOPIC/SUB-TOPIC: Exception Handling
TOTAL STUDENTS:06
PRESENT:
ABSENT:
2.
What you willlearn ??
Types of errors in Python
Concept of Syntax error
Concept of Exception
Types of Exception
How to handle exception
Applying the concept of Exception handling in making
Python programs
3.
Teaching Learning Outcomes-
Will have the idea about types of exceptions
during program execution
Students will be able to write Python code to
handle various kind of exceptions during
programming.
4.
Exception in reallife??
In general –
someone or something that is not
included in a rule, group, or list, or
that does not behave in the
expected way
5.
Error in Python
Errorin Python can be of two types –
Syntax errors and Exceptions.
Syntax errors - Errors are problems in a program due to
which the program will stop the execution.
amount = 10000
if(amount > 2999)
print("You are eligible to purchase Dsa
Self Paced”):
6.
Exceptions.
Exceptions are raisedwhen the program is syntactically
correct, but some internal events occur in the code which
change the normal flow of the program.
This error does not stop the execution of the program,
however.
Example: marks = 10000
b=0
a = marks / b
print(a)
Here in this code as we are dividing the ‘marks’ by zero so
an error will occur known as ‘ZeroDivisionError’
7.
Different types ofexceptions in python:
SyntaxError: This exception is raised when the interpreter encounters a
syntax error in the code, such as a misspelled keyword, a missing colon, or an
unbalanced parenthesis.
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.
NameError: This exception is raised when a variable or function name is not
found in the current scope.
IndexError: This exception is raised when an index is out of range for a list,
tuple, or other sequence types.
EOFError: This exception is raised when end of file occurs during reading the
content of file.
ZeroDivisionError: This exception is raised when an attempt is made to divide
a number by zero.
ImportError: This exception is raised when an import statement fails to find or
load a module.
8.
Catching Exceptions with
try– except – else - finally Statement –
try:
# Statements that can raise exceptions are kept inside the try
clause
except:
# statements that handle the exception are written inside except
clause.
else:
# This code executes if no exception
finally:
# This code always executes
def fun(a):
if a< 4:
b = a/(a-3)
print("Value of b = ", b)
try:
#fun(3)
fun(5)
except ZeroDivisionError:
print("ZeroDivisionError Occurred and Handled")
except NameError:
print("NameError Occurred and Handled")
else:
print("Else block. No exception occurred")
finally:
print("Finally program executed")
SOME EXAMPLES-
Advantages of ExceptionHandling:
•Improved program reliability: By handling exceptions
properly, you can prevent your program from crashing or
producing incorrect results due to unexpected errors or
input.
•Simplified error handling: Exception handling allows you
to separate error handling code from the main program
logic, making it easier to read and maintain your code.
•Cleaner code: With exception handling, you can avoid
using complex conditional statements to check for errors,
leading to cleaner and more readable code.
•Easier debugging: When an exception is raised, the
Python interpreter prints a traceback that shows the exact
location where the exception occurred, making it easier to
debug your code.
15.
Disadvantages of ExceptionHandling:
Performance overhead: Exception handling can be slower than using
conditional statements to check for errors, as the interpreter has to
perform additional work to catch and handle the exception.
Increased code complexity: Exception handling can make your code more
complex, especially if you have to handle multiple types of exceptions or
implement complex error handling logic.
Possible security risks: Improperly handled exceptions can potentially
reveal sensitive information or create security vulnerabilities in your code,
so it’s important to handle exceptions carefully and avoid exposing too
much information about your program.
16.
Recapitulation-
Questions from Previousyear / CBSE Sample papers
1. When will the else part of try-except-else be executed?
a) always
b) when an exception occurs
c) when no exception occurs
d) when an exception occurs in to except block
2. When is the finally block executed?
a) when there is no exception
b) when there is an exception
c) only if some condition that has been specified is satisfied
d) always
3. State whether the following statement is True or False:
An exception may be raised even if the program is syntactically correct
4. Which of the following statements is false?
a) A try-except block can have more than one except statement
b) One block of except statement cannot handle multiple exceptions
c) The finally block is always executed
d) When 1 == "1" is executed, no exception is raised
HOMEWORK-
Write a programto count number of
records in a binary file sports.dat by using
the EOFError exception.
The data is stored in the form of a list
[id,name, gamename,state]