EXCEPTION HANDLING

In this tutorial we will learn about exception handling in python programming , how to create them  ,use them etc .

therefore learn about its definition , try , catch , finally blocks .

 

exception

reference:techbeamers.com

Exception Handling

An exception is an abnormal condition in a program that disrupts the  normal flow of the program.

Therefore, an exceptions are  the error which python script is unable to tackle with.

in addition ,Python provides us with the way to handle the Exceptions so that the other part of the code can be executed without any disruption.

therefore, if we do not handle the exceptions, the interpreter doesn’t execute all the code that exists after the that.

therefore ,a example is zerodivisionerror

Need of exception

When we write a program, there might be some possible errors that can happen down the line. If there is no backup mechanism to handle these errors, the program will simply crash.

therefore ,Python exception handling is a mechanism in which we define a backup plan for a possible error situation.

Types of exception in python

there are two types of exception :

  • In-built exception :

Built-in exceptions are  pre-define in Python. occur when the program encounters an illegal operation like semantic errors. therefore ,when errors come system gives error and a error message .

Handling Exceptions in Python: try, except and finally

Exceptions are handled in Python using try statement where suspicious codes that may raise exceptions are placed inside the try block and the code that handle the raised exception is placed inside except block.

therefore ,structure is  :

try:
** Operational/Suspicious Code
except SomeException:
** Code to handle the exception

The try and except statements work as following.

  • First statements in try block are executed to check whether or not exception occurs
  • when exception doesn’t occur then the except block (except block contains statements to handle exceptions) is skipped after the execution of try block
  • If an exception occurs and matches the name defined some exception to handle in exception block, it is handled and the program continues.
  • when no corresponding handler for the exception is found in except block, the program execution stops with the corresponding error message.

 Except clause with no exception

try:
#do your operations
except:
#If there is any exceptions raised, execute these statements
else:
#If there is no exceptions, execute these statements

Example with no particular exceptions

try:
fp = open(‘example.txt’, r)
except:
print (‘File is not found’)
fp.close

Except clause with multiple exceptions

A try statement can have multiple clauses to specify handlers of different exception.

We can define multiple exceptions with the same except clause. therefore ,Later during execution, if interpreter finds a matching exceptions, therefore, it’ll execute the code written under the except clause.

 Example to handle multiple exceptions

try and except blocks, we can also use try and finally blocks together.

in addition ,The finally block contains statements that must be executed whether or not the exceptions is caught and raised in the try block.

therefore lets see example ,

try:
# do something
except (Exception1, Exceptions2, …, ExceptionN):
# handle multiple exceptions
pass
except:
# handle all other exceptions

How to use try-finally clause

try  and except  blocks, we can also use try and finally blocks together.

therefore,The finally  block have statements that will execute is exceptions comes or not.

syntax:

try:

# perform operations

finally:

# execute

therefore  ,example :

try:

fp = open(“example.txt”,’r’) #file operations

finally:

fp.close()

  • user define exceptions :As the name goes by the exceptions defined by a user ,therefore are called User-Defined Exceptions.

therefore ,Sometimes in a program, we need create custom exceptions to serve our own purposes.

in addition ,Python also allows us to create our own exceptions by deriving classes from the standard built-in exceptions.

in addition we have example :

class ErrorInCode(Exception):    

    def __init__(self, data):    

        self.data = data    

    def __str__(self):    

        return repr(self.data)    

    

try:    

    raise ErrorInCode(2000)    

except ErrorInCode as ae:    

    print(“Received error:”, ae.data)

therefore ,output is :

received error :2000

therefore , in end i will say that we have covered topics like try , catch , finally , definition of exception, in addition with its types. therefore we have covered user define exceptions , in addition with built in exceptions etc .this is all about exceptions . we have tried to cover all topics here with syntax , examples use of exceptions  etc etc .

https://javatpoint.com

%d bloggers like this: