Become a coder is very easy things but become a good coder you must know how to handle the error in the program during execution.so if you want to handle the error in code you must follow the try and except in code. When the program run and got the run time error it will not execute the script print then you have to use try/except/else/final command to get the output according to command.
Common Exceptions:
Python provides the number of built-in exceptions, but here we are describing the common standard exceptions. A list of common exceptions that can be thrown from a standard Python program is given below.
ZeroDivisionError:
This occurs when a number is divided by zero.
NameError:
It occurs when a name is not found. It may be local or global.
IndentationError:
If the incorrect indentation is given.
IOError:
It occurs when the Input-Output operation fails.
EOFError:
It occurs when the end of the file is reached, and yet operations are being performed.
example:When you divide the number by any by zero then python gives the zero devision error.
try:
num1= int(input("Enter num1:"))
num2= int(input("Enter num2:"))
num= a/b
except:
print("Can't divide with zero")
output:
Enter num1:10
Enter num2:0
Can't divide with zero
Declaring Multiple Exceptions: The Python allows us to declare the multiple exceptions with the except clause.
try:
#block of code
except (<Exception 1>,<Exception 2>,<Exception 3>,...<Exception n>)
#block of code
else:
#block of code
example:
try:
a=10/0;
except(ArithmeticError, IOError):
print("Arithmetic Exception occur")
else:
print("Successfully Done it")
output:
Arithmetic Exception
The try & finally block:
Python provides the optional finally statement, which is used with the try statement. It is executed no matter what exception occurs and used to release the external resource. The finally block provides a guarantee of the execution.
syntax:
try:
# block of code
# this may throw an exception
finally:
# block of code
# this will always be executed
example:
try:
fileptr = open("file2.txt","r")
try:
fileptr.write("Hi I am good")
finally:
fileptr.close()
print("file closed")
except:
print("Error")
output:
file closed
Error
Raising exceptions:
An exception can be raised forcefully by using the raise clause in Python. It is useful in that scenario where we need to raise an exception to stop the execution of the program. eg: I have 4gb memory and when it occupied then it raises an exception.
syntax:
raise Exception_class,<value>
example:
try:
age = int(input("Enter the age in number:"))
if(age<18):
raise ValueError
else:
print("the age is a valid value ")
except ValueError:
print("The age is not a valid value ")
output:
Enter the age:17
The age is not a valid value
example:
try:
num1 = int(input("Enter num1:"))
num2 = int(input("Enter num2:"))
if num2 is 0:
raise ArithmeticError
else:
print("num1/num2 = ",num1/num2)
except ArithmeticError:
print("The value of b can't be 0")
output:
Enter a:10
Enter b:0
The value of b can't be 0
Custom Exception:
The Python allows us to create our exceptions that can be raised from the program and caught using the except clause. However, we suggest you read this section after visiting the Python object and classes.
example:
class ErrorInCode(Exception):
def __init__(self, data):
self.data = data
def __str__(self):
return repr(self.data)
try:
raise ErrorInCode(4000)
except ErrorInCode as ae:
print("Received error:", ae.data)
output:
Received error: 4000
No comments:
Post a Comment