The exception is a one type of error that occurs during the runtime . when exception occurs the normal flow of execution is gets stop. and print the error message . in python provides the exception handlers like try and except. it prints the what type of error occurs in that particular code. and the it used to print the error message it prevent the crash of a program .
Some Reasons to occur a error that is in the below
The python provides the standard set of built in exception to raised the error during the run time . it prevent the code to crash a program
The raised exception when the no defined variable to print the value.
print(myName)
myName="Darshan"
print(myName)
The Exception raised during the invalid input received. When the user to get the input value as wrong then exception raised value error
int(" hello)
str("Hello')
The exception raised when the value that variable can store the incorrect value
a=hello
a="hello"
The Indentation Error exception is raised when the python code having a incorrect indent as block of code
For i in range(1,10):
print(i)
for i in range(1,10):
print(i)
The Error exception raised when the key is not found in the existing dictionary.
dist={"name"="darshan", "age"=19, 'place"="kolar"} print(address)
dist={"name"="darshan", "age"=19, 'place"="kolar"} print(place)
The Exception raised when we want to divide the any integer value ton zero . the ZeroDivisionError is appear
print(10/0)
print(10/5)
The Error Exception raised when we want to access the invalid range of index in the existing sequence type.
list=[1,2,3,4]
print(list[5])
list=[1,2,3,4]
print(list[3])
The python raised the error Exception when the file is not working properly or not found
The error exception raised when the module name is wrong then the import cannot be work properly.
Exception handling is a process or mechanism to handle the error may occur during the runtime . the exception make the program to execute continuously . and when the error occur it shows the informative of that particular error type .
• Preventing crashes
• Provide the informative about error
• Continue for execution
• Handing external conditions
The Exception handling try and Except is a keywords to gather and raised the exception if error may occurs.
Try it is a keyword it contains a block of code that raises an exception.
Except It is also Keyword it Contains a type of exception it may occur. Small Example in the below
try:
# code for exception raised.
except Exceptiontype:
# code of Exception type
Try:
list=[1,2,3,]
print(list[10])
except IndexError:
print("The index value does not exist")
The Exception handling try and Except,else,finally is a keywords to gather and raised the exception if error may occurs.
Try it is a keyword it contains a block of code that raises an exception.
Except It is also Keyword it Contains a type of exception it may occur.
Else The else contain if error does not exist
Finally It contains a This block of code always executed
try:
# code for exception raised.
except Exceptiontype:
# code of Exception type
else:
# code of exception does not exist.
Try:
list=[1,2,3,]
print(list[10])
except IndexError:
print("The index value does not exist")
else:
print("The code can execute with correct index")
The Exception handling try and Except is a keywords to gather and raised the exception if error may occurs.
Try it is a keyword it contains a block of code that raises an exception.
Except It is also Keyword it Contains a type of exception it may occur.
Else The else contain if error does not exist
try:
# code for exception raised.
except Exceptiontype:
# code of Exception type
else:
# code of exception does not exist.
finally:
# the Code contain the block of code is always executed
Try:
list=[1,2,3,]
print(list[10])
except IndexError:
print("The index value does not exist")
else:
print("The code can execute with correct index")
finally:
print("The statement is always executed")
The Raise is a keyword it is used to raise the exception manually
raise Exceptiontype("error message")
try:
age=int(input('Enter your age'))
if age<=0:
raise ValueError("Age cannot be less Then 0")
print(f'your age is{age}')
except ValueError as e:
print(e)