Exception Handling in Python By S P Siddique
Exception Handling in Python By S. P. Siddique Ibrahim AP/CSE
Introduction �How often you wrote the program without error. �First Attempt �Some errors in the program always.
Error �An error is something that can produce incorrect output (or) make the system to crash.
Types �Compile �Run Time Error
Compile Time Error �Errors that occur when you violate the rules of writing syntax are known as Compile-Time errors. �This compiler error indicates something that must be fixed before the code can be compiled. �All these errors are detected by the compiler and thus are known as compiletime errors.
Most frequent Compile-Time errors are: �Missing colon (: ) �Printing the value of variable without declaring it. �Missing semicolon (terminator) �Indentation �Rule violation
Exception �An Exception is an error that occurs at run time. [while program is running]
Exception Handling �It deals with such kind of errors without terminating the program
Run Time Error Types(Exception �Zero. Division. Error �File. Not. Found. Error �Index. Error �Performing an operation on incompatible types
Contd. , �Exception is a condition that prevents the execution of statements normally. �If exceptions are not handled, the program will terminate abnormally.
Example for Zero. Division. Error �a=int(input("Enter the first Number: ")) �b=int(input("Enter the second Number: ")) �c=a/b �print("a=: ", a) �print("b=: ", b) �print("a/b: ", c)
Handling Exception �In Python exception handling managed by three keywords: �try �except �finally
Syntax � try: � # Body of try block � except. Exception_type 1 � #perform of some kind of exception handling � # it is Exception Handler for Exception Type 1, Alerts the user � except. Exception_type 2 � ………. . � except. Exception_type. N
�The program statements that you want to monitor for exception should be in try block. �If an exception occurs within try block, then the particular exception is raised(or thrown). �The type of the exception in try block determines which except statement needs to be executed. [All others are bypassed] �If no exception is raised or thrown, then a try block ends normally, and all of its except statements are bypassed.
When error occurs in a program, exception performs the following tasks: �Find the Problem[Get �Informs an error has It] occurred[Raise the exception] �Receives the error information [the except statement] �Takes the remedial actions. [Handle the exception]
Example Programs �Handling Arithmetic Exception �Multiple except Blocks �The try-except-finally blocks
Finally block �Sometime referred to as finally clause. �It consist of final keyword. �The finally block is placed after the last except block. �If there is no except block, the finally block should immediately follow the try block.
Syntax �try: �# Body of try block �except. Exception_type 1 �except. Exception_type 2 �………. . �except. Exception_type. N �Finally: �-----------
Modules �In Python, Modules are smaller pieces of bigger program. �Each modules must saved with extension of. py �If you want to use the module then will import keyword help you to include the module.
Writing a Module �Writing a module is like simple python program and saving it in. py extension. �Module contains definition of function, variables and other action to be performed which can be utilized in other programs.
Lets us create a simple file �def add(): ◦ a=5 ◦ b=6 ◦ c=a+b ◦ print(“The addition of a and b is: ”, c)
�If we try to execute the above code, nothing will happen and the empty output screen will be produced by the python interpreter. �Since, we just written the definition of the function and it has not been called from elsewhere to perform its action.
�So let create a another file called main. py. �Here we can import the module_add. py which we just created and call the add function inside the program.
Packages in Python �Multiple modules together in order to form a package. �Package is actual a directory which contains _init_. py file and other(Multiple) modules.
Creating Package Folder Name My_package _init_ a. py b. py
Steps to create package �Step 1: Create a package folder in your computer(Inside the python native folder) �Step 2: Create a _init_. py empty file inside the newly created folder �Step 3: Inside the package, lets create two different. py files i. e a. py and b. py
� Step 4: � Step 5: Write some contents inside the a. py and b. py open interactive python shell and type the following � >>> import my_package. a # my_packge is the folder name � � >>> import my_package. b � � >>> my_package. a. A() � � Inside A function: � >>> my_package. b. B() � � Inside B Function:
- Slides: 28