TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
Exception Handling in Python
1. Exception Handling in Python
Dr.G.Jasmine Beulah
Assistant Professor, Dept.Computer Science,
Kristu Jayanti College,Bengaluru
2. What is an Exception?
• An exception in Python is an incident that happens while
executing a program that causes the regular course of the
program's commands to be disrupted.
• When a Python code comes across a condition it can't handle, it
raises an exception. An object in Python that describes an error
is called an exception.
• When a Python code throws an exception, it has two options:
handle the exception immediately or stop and quit.
3. # Python code to catch an exception and han
dle it using try and except code blocks
a = ["Python", "Exceptions", "try and except"]
try:
#looping through the elements of the array a, choosing a range that
goes beyond the length of the array
for i in range( 4 ):
print( "The index and element from the array is", i, a[i] )
#if an error occurs in the try block, then except block will be executed
by the Python interpreter
except:
print ("Index out of range")
4.
5. How to Raise an Exception
• If a condition does not meet our criteria but is correct according
to the Python interpreter, we can intentionally raise an exception
using the raise keyword. We can use a customized exception in
conjunction with the statement.
• If we wish to use raise to generate an exception when a given
condition happens, we may do so as follows:
6. #Python code to show how to raise an except
ion in Python
num = [3, 4, 5, 7]
if len(num) > 3:
raise Exception( f"Length of the given list must be less than or e
qual to 3 but is {len(num)}" )
7. Assertions in Python
• Assertions are commonly used at the beginning of a function to
inspect for valid input and at the end of calling the function to
inspect for valid output.
8. The assert Statement
• Python examines the adjacent expression, preferably true when
it finds an assert statement. Python throws an AssertionError
exception if the result of the expression is false.
• The syntax for the assert clause is −
assert Expressions[, Argument]
Python uses ArgumentException, if the assertion fails, as the
argument for the AssertionError.
We can use the try-except clause to catch and handle
AssertionError exceptions, but if they aren't, the program will
stop, and the Python interpreter will generate a traceback.
9. #Python program to show how to use assert key
word
# defining a function
def square_root( Number ):
assert ( Number < 0), "Give a positive integer"
return Number**(1/2)
#Calling function and passing the values
print( square_root( 36 ) )
print( square_root( -36 ) )
10. What is Assertion?
• Assertions are statements that assert or state a
fact confidently in your program. For example,
while writing a division function, you're confident
the divisor shouldn't be zero, you assert divisor is
not equal to zero.
• Assertions are simply boolean expressions that
check if the conditions return true or not. If it is
true, the program does nothing and moves to the
next line of code. However, if it's false, the
program stops and throws an error.
• It is also a debugging tool as it halts the program
as soon as an error occurs and displays it.
11. Python assert Statement
• Python has built-in assert statement
to use assertion condition in the
program. assert statement has a
condition or expression which is
supposed to be always true.
• If the condition is false assert halts
the program and gives an
AssertionError.
Syntax for using Assert in Pyhton:
assert <condition>
assert <condition>,<error message>
In Python we can use assert statement
in two ways as follows:
1.assert statement has a condition and
if the condition is not satisfied the
program will stop and give
AssertionError.
2.assert statement can also have a
condition and a optional error message.
If the condition is not satisfied assert
stops the program and gives
AssertionError along with the error
message.
13. Example 1: Using assert without Error Message
def avg(marks):
assert len(marks) != 0
return sum(marks)/len(marks)
mark1 = []
print("Average of mark1:",avg(mark1))
14. Example 2: Using assert with error message
def avg(marks):
assert len(marks) != 0,"List is empty."
return sum(marks)/len(marks)
mark2 = [55,88,78,90,79]
print("Average of mark2:",avg(mark2))
mark1 = []
print("Average of mark1:",avg(mark1))
15. Key Points to Remember
• Assertions are the condition or boolean expression which are always
supposed to be true in the code.
• assert statement takes an expression and optional message.
• assert statement is used to check types, values of argument and the
output of the function.
• assert statement is used as debugging tool as it halts the program at
the point where an error occurs.
16. Try with Else Clause
• Python also supports the else clause, which should come after
every except clause, in the try, and except blocks.
• Only when the try clause fails to throw an exception the Python
interpreter goes on to the else block.
17. # Python program to show how to use else cl
ause with try and except clauses
# Defining a function which returns reciprocal of a number
def reciprocal( num1 ):
try:
reci = 1 / num1
except ZeroDivisionError:
print( "We cannot divide by zero" )
else:
print ( reci )
# Calling the function and passing values
reciprocal( 4 )
reciprocal( 0 )
18. Finally Keyword in Python
• The finally keyword is available in Python, and it is always used
after the try-except block.
• The finally code block is always executed after the try block has
terminated normally or after the try block has terminated for
some other reason.
19. # Python code to show the use of finally claus
e
# Raising an exception in try block
try:
div = 4 // 0
print( div )
# this block will handle the exception raised
except ZeroDivisionError:
print( "Atepting to divide by zero" )
# this will always be executed no matter exception is raised or not
finally:
print( 'This is code of finally clause' )
20. User-Defined Exceptions
class EmptyError( RuntimeError ):
def __init__(self, argument):
self.arguments = argument
Once the preceding class has been created, the following is how to rai
se an
exception:
Code
var = " "
try:
raise EmptyError( "The variable is empty" )
except (EmptyError, var):
print( var.arguments )