Introduction:
In the Internship, from past month, I am working with the team that develops APIs(python scripts) to expose Callidus Configure Price Quote(CPQ) data to Deal Approval mobile application used by Account Executives. These APIs were initially developed by other team and later it was taken over by my team for further developments. All the APIs are very huge, and which is causing us difficulty in maintaining it. Hence has decided to redesign the APIs, which are more readable and maintainable. Three of the team members are selected to redesign the APIs, and I am one of them. Hence to prepare myself for the upcoming tasks, I will be going through few topics that will help me in redesigning APIs.
Following are the list of topics I will go through in the coming days:
- Python OO
- Exception handling
- Most used Design patterns
- Design principles(SOLID)
Exception Handling
What is Exception?
An exception is an event that occurs during the execution of a program which disrupts the normal flow of execution of the program. An exception is a Python object that signifies an error. When a Python script raises an exception, it must either handle the exception handling or else the program will terminate abruptly.
Some of the common Exceptions:
|
Exception |
Base class for all exceptions |
| ArithmeticError | Errors related to numeric calculations |
| ZeroDivisionError | Raised when the divisor is Zero in division |
| IOError | Raised for input and output related errors |
| ValueError | Raised when arguments have invalid data |
| RuntimeError | Falls into this category when an error does not fall into another category |
| TypeError |
Raised for the invalid data type |
What is Exception handling?
Exception handling is the process of responding to exceptions when a program runs. It attempts to gracefully handle those situations so that a program does not end abruptly.
Python handles exception using try, except block.
Syntax:

Try: Try block contains logic. Each try block can have multiple exception handlers. This becomes useful when the code inside try block can raise multiple exceptions.
Except: This block contains logic to handle the exception.
Else: If no exception is raised by try block, then logic inside else part gets executed.
Example:

Case 1:
a = 10
b = 0
In this case, since the divisor is zero, an exception ‘ZeroDivisionError’ is raised, and “Divisor cannot be Zero” is printed.
Case 2:
a = 10
b = 2
In this case, no exception is raised, else part is executed and “Success” is printed.
Finally clause:
Finally block is executed, irrespective of whether an exception is raised or not.
Example:
Fileop.close() will be execute if exception is raised or not.
Raising an Exception:
An exception can be raised by using “raise” statement.
Example:

Based on certain conditions exception will be raised. The important point here is that, whatever error is raised, except should be able to handle it.
User-Defined Exceptions:
Python allows creating your own exception by deriving classes from standard exceptions.
Example:

In the try block, the user-defined exception is raised and caught in the except block. In the exception block, “err” object of a user-defined exception is created.