introduction image

The word exception  is used to refer to  an unexpected event or situation. 

In programming, an exception refers to a situation which is not part of the normal operation of a program. Exceptions  occurs when there are errors in the program or when unexpected values or results are encountered.

In Python when an exception is encountered, the program immediately comes to a halt except in cases where we have explicitly handled the exception. You can check on Exception handling to see how exceptions are handled.

Terms associated with exceptions

The following list contains important terms that are associated with exceptions:

  1. Exception throwing- Whenever an error occurs in a program, an exception is thrown. The goal of exception throwing is to alert the programmer so they can identify and address the error.
  2. Exception Handling -  Exception handling involves catching an exception when it is raised, and then responding to it by taking a corrective action. This may involve executing some alternative code,  logging the error, or prompting the user to handle the issue. It can also be referred to as exception catching. 
  3. Exception Propagation -   Exception propagation occurs when an exception is propagated up the call stack. It typically happens when an error is thrown somewhere lower in the call stack and not handled, so it is passed up the call stack to the next calling object. If the higher up object cannot handle the exception either, the exception is further propagated up the call stack until it is handled or until the program terminates.
  4. Stack Trace -   A stack trace is a report of the active stack frames when an exception is thrown. It shows the hierarchy of nested objects that were called when an exception was thrown. Each entry in the trace may include the object name, the file name, and the line number of the code that was being executed when the exception was thrown.
  5. Error Logging -    Error logging is the process of recording errors and other information that could be useful in diagnosing errors. Information that is typically logged includes the type of error, the time it occurred, and any relevant data associated with the error. The data can be saved in a file  or be sent as notifications to the program administrators.

Example of python exceptions

The most obvious exception and which you are likely to be  familiar with is the SyntaxError. This exception is usually caused by typos, missing punctuation, or, generally, incorrect structure of the python code. For example:

print("Hello World!"
a = [1, 2, 3, 4)

Another common exception occurs when you try to divide any number by 0.  It is known as the ZeroDivisionError.

print(5 / 0)
More examples:
print(1 + [1, 2, 3])
int("Hello, World!")

Why do exceptions exist?

Let us be frank, exceptions can be extremely annoying and frustrating.  This is especially the case in times when you have spent what seems like eternity trying to trace  the source of the problem and still no solution is in sight.  And even worse, when you have managed to  find the source, you modify the code and now you have guess what?, another exception. 

Despite all the headaches they cause, exceptions brings  some sanity in what would otherwise be  a chaotic programming environment. The following list illustrates why they are an invaluable feature in software:

  • They provide a way to gracefully handle errors and other exceptional events, thus improving the flow of the program.
  • They make debugging easier by providing a mechanism for pinpointing the exact source of any errors.
  • They make it easier to create robust code, as errors can be caught and handled in a uniform way.
  • They help to prevent any unnecessary damage that could be caused by an unexpected problem,
  • They can be used to prevent potential security vulnerabilities from occurring by catching malicious inputs.
  • They enforce good programming practices by ensuring that the application is well designed and potential errors are handled in advance.
  • They make programs more maintainable.