❮ Previous Next ❯

Python Raising an Exception(raise keyword)


Python Object Oriented Programming


Sometimes to fulfill the program requirements the programmer manually handover an
exception to control, for this in python we use raise keyword.

That means the raise keyword is specifically used to throw an exception explicitly.

try:

    raise ZeroDivisionError("division by zero")

except ZeroDivisionError as ae:

    print(ae)


Output

division by zero

here ZeroDivisionError exception explicitly I have thrown using raise keyword. As we know
all exceptions are inbuilt classes which are derived from generic class Exception. We can use
Exception also.


try:

    raise Exception("division by zero")

except Exception as ae:

    print(ae)

Output

division by zero

The raise keyword is widely used in custom exception. When we want to define our own
exception and throw that exception, then using raise keyword we will do which is written in
next tutorial.



❮ Previous Next ❯

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