Leran What is exception handling in python programin language. It is event which occurs during the execution of program and disturbs the flow of program.
The python script will encounter the situation that cannot come together, raise the exception.
It will represent the error from the program.
Also provides the important features to handle the error in python programming and add debugging.
They are convenient to use for handling errors and conditions in the program.
If the code produces an error then you can use the exception handling.
The python programming language has many build in exceptions that force your program to give output error when anything is wrong.
Whenever the exception occurs the current process is stopped and passes to the calling process until it handles.
If not handled then the program will totally crash.
Python language has many build in exceptions as follows,
Example:-
Def authenticate user (request):
Try:
Email=request.data [‘username’]
Password=request.data [‘password’]
user=authenticate (email=email, password=password)
if user and user. is active:
return response (user details, status=HTTP_200_OK)
else:
result= {‘error:’ can not authenticate with the given credentials’}
return response (res, status=status.HTTP_400_BAD_REQUEST)
except KeyError:
res= {‘error’:’please provide a username and a password’}
The exception is raised when the code is not properly indented and instead of using the curly braces python will use the indentation and get the indentation correct.
def is even(number):
if number %2==0:
print(“%s is an even number”%(number))
else:
print(“%s is an odd number”%(number))
print(is even(1))
print(is even(2))
Output:-
IndentationError: expected an indented block
It is common type of the exception in the python language and occurs when there is an error in the syntax of code.
Example:-
def is odd (n)
if n%2!=0:
print(it’s odd)
else:
print(“not odd”)
print(is odd(7))
The output of this code is the invalid syntax at 3 line because “I’s odd”
Is not enclosed.
Type Error:-
The exception is raised if operation us attempted which is invalid for the specified data type.
Example:-
Def sum of numbers (a, b):
Return a+b
Print(sum of numbers (1, 2, 7))
Output:-
$ pythoin sum_of_numbers.py
Traceback (most recent call last):
File “sum_of numbers.py”, line 4, and <module>
Print(sum_of_numbers (1, 2, 7))
Example:-
If function A will call function B which turn the call function C and the exception will occur in function C.
If the exception is not handled in C then it will pass to B and then to A.
Again if it is not handled then the error message is split out and program will take an halt.
Error in python language:-
Errors mean the mistakes in code which are harmful for the program.
Exception class |
Event |
Arithmetic Error |
It raises when numerical operation fails. |
Io error |
Gets raise when I/O operation will fail |
Floating point error |
It raises when floating operation fails. |
Zero Division error |
Raises when division by zero take place for all numeric values |
Assertion error |
It raises when assert statement will fail. |
Overflow error |
It raise when arithmetic operation is too large to represent. |
Import error |
Gets raised when imported value module is absent. |
Index error |
It raise when index of sequence is out of range. |
Name Error |
Gets raised if identifier is not found in local namespace. |
Value error |
Gets raised when function gets an argument of correct type but improper value. |
Runtime error |
Gets raised when the error do not fall into any category. |