Next ❯ ❮ Previous

finally keyword

Like try and except, finally is a block basically containing cleanup code. That means finally block must execute whether exception is handled or not handled.

try:

    x=10/0

except FileNotFoundError as e:

    print(e)

finally:

    print(10//2)

print("Python means SILAN Technology")


Output
5

Traceback (most recent call last):

    File "E:/python programs/ExceptionExample5.py", line 2, in <module>

    x=10/0

ZeroDivisionError: division by zero

Here exception is not handled but that does not affect on finally block. Once finally block present in a program, first control will execute finally part then raise exception which is shown.

Now let’s see the following code:

try:

    x=10/0

except ZeroDivisionError as e:

    print(e)

finally:

    print(10//2)

print("Python means SILAN Technology")


Output
division by zero
5

Python means SILAN Technology

Here in this source code, happily exception handled, and since finally block present that part also executed.

So from the above, we observed that once finally block present in a program, it must execute without any fail.

Next ❯ ❮ Previous

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved