Social Media

Image Slider

Showing posts with label Exception. Show all posts
Showing posts with label Exception. Show all posts
2/11/2019

Explicitly raising and handling of Exception

Explicitly raising and implicitly handling:

x=int(input("Enter first number "))
y=int(input("Enter second number "))
if y==0:
    raise ZeroDivisionError("Cannot divide by zero")
rslt=x/y
print("The result is",rslt)

if y=0 then output of above script can be as:
Enter first number 1
Enter second number 0
Traceback (most recent call last):
  File "/root/Desktop/p1.py", line 4, in <module>
    raise ZeroDivisionError("Cannot divide by zero")
ZeroDivisionError: Cannot divide by zero # message will be printed here


Explicitly raising and implicitly handling:

a=int(input("Enter first number "))
b=int(input("Enter second number "))
try:
    if b==0:
        raise ZeroDivisionError("Cannot divide by zero")
    rslt=a/b
    print("The result is",rslt)
except ZeroDivisionError as e:
    print("This is exception:",e)
finally:
    print("This program is created by Mayank Kumar Prajapati")


if b=0 then output of above script is as:
Enter first number 1
Enter second number 0
This is exception: Cannot divide by zero
This program is created by Mayank Kumar Prajapati


Implicitly raising and handling of Exception

Implicitly raising and implicitly handling:

a=int(input("Enter first number "))
b=int(input("Enter second number "))
rslt=a//b
print("The result is",rslt)


If b=0 then result can be as:
Enter first number 2
Enter second number 0
Traceback (most recent call last):
  File "/root/Desktop/p1.py", line 3, in <module>
    rslt=a//b
ZeroDivisionError: integer division or modulo by zero


if b!=0 then result will be:
Enter first number 2
Enter second number 1
The result is 2


Implicitly raising and explicitly handling:

a=int(input("Enter first number "))
b=int(input("Enter second number "))
try:
    rslt=a//b
    print("The result is",rslt)
except ZeroDivisionError as e:
    print(e)
else:
    print("Program executed successfully")
finally:
    print("This program is created by Mayank Kumar Prajapati")


if b=0 then the result of b=above program is:
Enter first number 2
Enter second number 0
integer division or modulo by zero
This program is created by Mayank Kumar Prajapati


if b!=0 then result is:
Enter first number 2 
Enter second number 1
The result is 2
Program executed successfully
This program is created by Mayank Kumar Prajapati

2/10/2019

try, except and finally

In try block we have to write that code of our program, where we expecting that exception will come.

Some points about try and except:
  • We cannot have any except block without try block.
  • In except we have to catch the object thrown by that line which is present in the try block.
  • There can be more than one except block for a single try block.
  • except block only executed when any exception occurs, if no exception occurs program will execute normally and don't enter in the except block.
  •  If we have not made any except block, then python will handle exception automatically.
  • We can also accept more than one exception in the except by writing the name of the class separated by the ",". 
Some points about finally block:
  • This block always be executed, it doesn't matter whether the exception is occurred or not in our program.
  • finally always come after the except block.
  • finally works before the default exception catch mechanism of python.
  • try block should have at least one except or finally block.
  • We can't made more than one finally block.
  • If we made except by ourselves and exception occurs, then in this case, first except block will be executed and then finally block executed.
Note* We can also use else block with try block. else block will execute when there is no exception occurs in our program.
We have to write the else block before finally block and after except block.


try:


   """ write your code here""" 

except ExceptionClassName1: 

   """Write your code here""" 


except ExceptionClassName2: 

   """Write your code here" 

else:   

   """Write your code here"""

finally:

    """Write your code here"""
2/09/2019

Exception Handling

When a program terminates in abnormal condition, it is called exception.
The normal flow of the program is disturbed if any exception situation occurs in the program. The simplest example of exception situation is as:
e.g. Suppose we have to calculate a/b where a and b are entered by the user. What will  happen if user unknowingly enter the value of b 0. Exception will arise, because we can't divide any number by 0.

Our program will terminate from that line where exception is occurred.
Python has mechanism to deal such kinds of situation. We have 4 ways to deal with these kinds of situation.
  1. Python raise exception and also handle it implicitely
  2. Python raise raise exception and user handle it explicitely
  3. User raise exception and python handle it implicitely
  4. User raise exception and also handle it explicitely
 Exceptions can be of two types.
  1. Predefined Exceptions
  2. User defined Exceptions
The keywords that we will use in Exception handling are as follows:
  1. try
  2. except
  3. finally
  4. raise