Good Exceptions Notes
Good Exceptions Notes
Error:
An error is an action which is inaccurate or incorrect.
Error handling increases the robustness of the code
----- As it safe guards against potential failures that would
cause your program to exit in an uncontrolled fashion.
-------
Exceptions
Elaboration:
1.Error
Syntax errors often called as parsing errors are predominantly
caused when the parser/interpreter detects a syntactic issue
in our code.
Let's take an example to understand it.
a=8
b = 10
c=ab
File "<ipython-input-8-3b3ffcedf995>", line 3
c=ab
^
SyntaxError: invalid syntax
Memory errors are mostly dependent on your systems RAM and
are related to Heap. If you have objects larger than your memory
size, then you will see OutofMemoryError.
Example1:
x=int(input(“Enter the value of x”)
y= int(input(“Enter the value of y”)
Result=x/y
Print(“Result of Division is:”, Result)
X=2 y=1
Result=1
Example2:
4+apple*3
Traceback(most recent call last):
NameError: name ‘apple’ is not defined
Example3:
>>> ‘4’+ 3
Traceback(most recent call last):
TypeError: Can’t convert ‘int’ object to str implicitly
Exception Handling
The method/technique/piece of code adopted/written by
the programmer to handle the exceptions or run time errors
which are identified by the Interpreter is know as Exception
Handling
It is possible to write programs that handle exceptions by using the
Try, except,else,finally block.
Example:
def division(a , b):
c =(a / b)
return c
a=float(input("Enter the value of a= "))
b=float(input("Enter the value of b= "))
Result=division(a, b)
Print(“The result of Division is :”,Result)
No exception Handling
OverFlow Error
The Overflow Error is raised when the result of an arithmetic
operation is out of range. OverflowError is raised for integers that
are outside a required range.
try: try:
import math import math
print(math.pi(10)) print(math.pi(10000000000))
except OverflowError: except OverflowError:
print ("OverFlow Exception print ("OverFlow Exception
Raised.") Raised.")
else: else:
print ("Success, no error!") print ("Success, no error!")
finally: finally:
print(“Program Executed”) print(“Program Executed”)
After Execution: After Execution:
Success, no error ! OverFlow Exception Raised.
Import Error
ImportError is raised when you try to import a module that does not
exist (unable to load) in its standard path or even when you make a
typo in the module's name.
try:
try:
import module1
import module1
except
except
ModuleNotFoundError:
ModuleNotFoundError:
print (" Module Not Found
print (" Module Not Found
Error Exception Raised.")
Error Exception Raised.")
else:
else:
print ("Module successfully
print ("Module successfully
Imported, no error!")
Imported, no error!")
finally:
finally:
print(“Program Executed”)
print(“Program Executed”)
After Execution:
After Execution:
Module Not Found
Module successfully Imported, no
Error Exception Raised.
error!
Lookup Error
Index Error
Key Error
If a key you are trying to access is not found in the dictionary,
a key error exception is raised.
a = {1:'a', 2:'b', 3:'c'} a = {1:'a', 2:'b', 3:'c'}
n= 1 n= 4
try: try:
print (a[n]) print (a[n])
except LookupError: except LookupError:
print ("Key Error Exception print ("Key Error Exception
Raised, Index out of range") Raised, Index out of range")
else: else:
print ("Success, no error!") print ("Success, no error!")
Finally: Finally:
Print(‘ Program successfully Print(‘ Program successfully
executed’) executed’)
Name Error
Name Error is raised when a local or global name is not found.
In the below example, Prime Minister variable is not defined. Hence,
you will get a name error.
try: try:
print (Prime Minister) print (Prime Minister)
except NameError: except NameError:
print ("NameError: name print ("NameError: name
‘Prime Minister’ is not ‘Prime Minister’ is not
defined") defined")
else: else:
print (" Name of our Prime print (" Name of our Prime
Minister is successfully Minister is successfully
printed") printed")