Python allows a very simple flow of creating custom exceptions using classes, these classes are inherited from the built-in exception class. Python has a wide range of built-in exceptions and apart from that users can raise custom exceptions that are sourced from the custom exception class. The custom exceptions can be stored in a separate exception.py file in the project directory to allow for easy access of the exceptions. Other than that, exceptions that are user defined can be used in a similar manner as built-in exceptions and can be raised using the raise() function.

Python is most often an implicitly defined programming language that uses an interpreter running line by line instead of compiling the entire program at once and raising the errors and exceptions. This makes way for much ease while programming but requires precision and accuracy of indentations, spacing and format as Python is extremely case-sensitive as it is dependent on implicit declarations. Here are some general examples of creating custom exception classes and raising them at the same time:

Class CError(Exception): #This represent a pillar of OOP which is inheritance as the custom class of CError is derived from Exception class

     pass #This will continue the code instead the exiting since the class is empty


try:
  raise CError #This will raise the exception
except CError:
  print("My own custom exception") #Here the exception can be further defined.
  

You can also print a message in the raise() block and then call it using a custom callback name.

Class CError(Exception): #This represent a pillar of OOP which is inheritance as the custom class of CError is derived from Exception class

     pass #This will continue the code instead the exiting since the class is empty


try:
  raise CError('This is my custom exception') #This will raise the exception
except CError as ce:
  print(ce) #Here the exception can be called using a custom callback name
  
Class WrongValue(Exception): #We have created an exception class which is inherited from the Exception class that is built-in with Python.

   pass #The pass will not stop the code here since the class is empty but will continue to the code


print("Enter a value that is larger than 2 but less than 6")

x = int(input("Enter Value: "))

try:
   if x < 2:
         raise WrongValue #This will call the exception we have defined in the initial class
   elif x > 6:
         raise WrongValue
   break

except WrongValue:
   print("The entered value is wrong") #except clause further adds some functionality or responsiveness to the exception class.

Multiple Exception Classes

Just like creating a single class, you can inherit multiple custom exception classes from the built-in exception class, it is quite an easy task and allows you to diversify your exception range for specific exception that you want to raise, it can be noticed that we used a try and except method to handle these exception, if we don’t handle exceptions, the program will simply exit after execution the exception and to avoid that we can use try and except and try, except and finally methods.

Considering the entire concept behind raising exceptions, multiple exceptions can present errors in themselves too, two classes must not have similar names and two classes must not overlap or over-crowd the parent class of Exception.

class ValueGreater(Exception):
   pass

class ValueSmaller(Exception): #Two Classes inherited from the same Exception Class, the Exception class depicts the function of raising an exception or an error.
   pass

x = int(input('Enter Integer Value for X: '))

try:

   if x < 2:
       raise ValueSmaller("The Entered Value is smaller than 2")
   elif x > 6:
       raise ValueGreater("The Entered Value is greater than 6")


except ValueGreater as vg: #The defined except method here uses a custom callback for the ValueGreater Class, we can also make multiple changes to the Class itself

   print(vg)

except ValueSmaller as vs:
   print(vs)

except ValueInvalid as vi:
   print(vi)

Apart from just using empty classes, you can define functions within the classes that check for error under specific conditions and the class can be called when that specific error occurs. This method can be utilised for extra personalisation or deeper tasks that require minute details about the exception.

Categorized in: