Exception Handling
Exception Handling
behaves abnormally. These occur when there are syntax errors, runtime
errors or logical errors in the code. In Python, exceptions are errors that get
triggered automatically.
Errors
Error is an abnormal condition. Whenever it occurs execution of the program is stopped.
Errors are mainly classified into following types:
• Compile Time Errors – Occur at the time of compilation of code.
Further classified into:
⮚ Syntax errors – Occurs when rules of a programming language is misused.
Eg : if a=5:
⮚ Semantics errors – Occurs when statements are not meaningful.
Eg: a+b=c
⮚ Type errors - If a function is given wrong type of data, then type error is assigned by
compiler.
Eg :
a=“Hi”
a**2 This will result in type error.
Errors
• Run Time Errors : A Run time error occurs during execution of the program due to
unexpected situations. Such errors are handled through exception handling routines of
Python.
For example :
• If a program is trying to open a file which does not exist.
• An expression is trying to divide a number by zero are RUN TIME ERRORS.
Eg:
ctr=1
while(ctr>10):
print(n *ctr)
ctr=ctr+1
Built-in Exceptions in Python
Exception Cause
SyntaxError Raised when there is an error in the syntax of the Python code
ValueError Raised when a built-in method or operation receives an argument that has the right
data type but mismatched or inappropriate values.
IOError Raised when the file specified in a program statement cannot be opened.
KeyboardInterrupt Raised when the user accidentally hits the Delete or Esc key while executing a program
due to which the normal flow of the program is interrupted
ImportError Raised when the requested module definition is not found
EOFError Raised when the end of file condition is reached without reading any data by input().
OverFlowError Raised when the result of a calculation exceeds the maximum limit for numeric data
type
KeyError Raised when a mapping (dictionary) key is not found in the set of existing keys
Terminology used within Exception Handling
Terminology Description
try block A set of code that might have an exception thrown in it.
Throwing or raising an error The process by which an exception is generated and passed
to the program
Except clause or catch block The block of code that attempts to deal with the exception
Try – Except example
Code Output
try:
# statements that may raise exception
except:
# handle exception here
finally:
# statements that will always run
Raising Exceptions
Programmers can forcefully raise exceptions in a program using the raise
and assert statements. Once an exception is raised, no further statement in
the current block of code is executed.
• The raise Statement - raise keyword is used to raise/force an exception.
That means, a programmer can force an exception to occur through raise
keyword.
Raising Exceptions
• The assert Statement - In some situations, the programmer has a
clear idea about the requirements and test-conditions required. So in
programs where it is known the likely results of some conditions and
where results being different from the expected results can crash the
programs, Python assert statement can be used if the condition is
resulting as expected or not. In other words, an assert statement in
Python is used to test an expression in the program code. If the result
after testing comes false, then the exception is raised.
print("Enter the Numerator: ")
n = int(input())
print("Enter the Denominator: ")
d = int(input())
assert d != 0, "Denominator must not be 0“
print("n/d =", int(n/d))