❮ Previous Next ❯

Python Customized / User-Defined Exception


When an exception is defined by the user based on the problem requirements is known as customized or user-defined exception. Sometimes we need to define customized exception to fulfill the program requirements. Here the user-defined exception has to be derived from Exception class directly or indirectly.

Here we have created two custom exceptions like TooOldException and TooYoungException which are derived from Exception class. Like other exceptions, these new exception can be raised using raise statement.


CustomException.py

class TooOldException(Exception):

  pass

class TooYoungException(Exception):

  pass

age=int(input("enter the age value"))

if age>60:

  raise TooOldException("Sorry!We can't help u")

elif age<18:

  raise TooYoungException("Sorry!U have to wait some years")

else:

  print("Welcome, asap we will provide best matched profile")

python customized or user-defined exception.


Here I have presented a logic for matrimonial application. If somebody will enter the age value greater than 60 then the application will throw an exception that is TooOldException, and if the age value is less than 18, then the application will throw an exception that is TooYoungException, otherwise application will give a positive response.

❮ 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