Exception Handling
Exception Handling
Syntax Errors - errors that are detected when we have not followed the rules while writing a
program or skip some syntax. These errors are also known as ‘parsing errors’. When a syntax error
is encountered, Python displays the name of the error and a small description about the error.
Eg:
1. a=10 2. a=10
if(a/2) print ”a”
print(“a is even”)
output: output:
SyntaxError SyntaxError
(colon ‘:’ is missing in (Parenthesis is missing in print
if statement)
Exceptions - is a Python object that represents an error. Exceptions are raised when the program
is syntactically correct, but the code results in an error. This error does not stop the execution of
the program, however, it changes the normal flow of the program.
Commonly occurring exceptions are usually defined in the compiler/interpreter. These are called
built-in exceptions.
Example:
raise Statement - Programmers can also forcefully raise exceptions in a program using the raise
statements. raising an exception involves interrupting the normal flow execution of program and
jumping to that part of the program (exception handler code) which is written to handle such
exceptional situations.
syntax:
raise exception-name[(optional argument)]
Handling Exceptions - technique that helps in capturing runtime errors and handling them to avoid
the program getting crashed. This is done by writing additional code in a program to give proper
messages or instructions to the user on encountering an exception. Exception Handling can be done
by try-except-else-finally method.
try - except : try statements are used to catch exceptions and except statements are used to handle
exceptions. Statements that can raise exceptions are wrapped inside the try block and the
statements that handle the exception are written inside except block. While executing the
program, if an exception is encountered, further execution of the code inside the try block is
stopped and the control is transferred to the except block.
try:
[ program statements where exceptions might occur]
except [exception-name]:
[ code for exception handling if the exception-name error is encountered]
Eg: The try block will generate an exception, because x is not defined:
try:
print(x)
except:
print("An exception occurred")
Eg: Here we are trying to access the list element. The second print statement tries to access the
fourth element of the list which is not there and this throws an exception. This exception is then
caught by the except statement.
a = [1, 2, 3]
try:
print ("Second element = %d" %(a[1]))
print ("Fourth element = %d" %(a[3]))
except:
print ("An error occurred")
output:
Second element = 2
An error occurred
Many Exceptions –
Eg: Print one message if the try block raises a NameError and another for other errors:
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
try-except-else - if there is no error in try block then none of the except blocks will be executed.
The statements inside the else clause will be executed.
Syntax:
try:
# Some Code....
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
Eg:
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
Output:
Hello #try executed
Nothing went wrong #else executed because no error in try
try-except-finally - will be executed regardless if the try block raises an error or not.
Syntax:
try:
# Some Code....
except:
# optional block
# Handling of exception (if required)
finally:
# Some code .....(always executed)
Eg:
try: # x is not defined
print(x)
except:
print("Something-went-wrong")
finally:
print("The 'try except' is finished")
output:
Something-went-wrong #except executed because x is not defined in try
The 'try except' is finished #finally executed