TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Programming Languages / Python / Software Development

Handling Edge Cases and Exceptions in Python

Properly handling exceptions and edge cases is crucial in developing robust and secure applications. This tutorial shows how.
Sep 28th, 2024 5:00am by
Featued image for: Handling Edge Cases and Exceptions in Python
Featured image via Unsplash.

Developing applications is all fun and games until the error messages start coming fast and furiously. Welcome to your exceptions and edge cases. It might seem intimidating at first but there is a way to handle exceptions and edge cases without the headache — because they’re going to happen, especially when the application has user input fields. The best way to troubleshoot these issues before they occur in a production-level application is to properly handle exceptions and edge cases.

What Are Exceptions and Edge Cases?

An exception is an error that occurs when an application encounters an unexpected situation. This interrupts the normal execution flow. Exceptions are situations that the program wasn’t designed to handle. Examples of common exceptions are FileNotFoundError, which occurs when the program tries to access a file that doesn’t exist, and TypeError, which arises when an operation is applied to an object of an inappropriate type.

An edge case represents an atypical boundary condition that, though rare, could cause the program to behave unexpectedly. An edge case could turn into an exception, but that’s not always the case. Examples of edge cases include empty inputs or lists in a function that processes data, processing the maximum integer value allowed by the system, and dealing with the first or last element in a loop sequence. If not handled correctly, edge cases can result in an error.

What Happens If We Don’t Handle Exceptions In the Development Phase?

When an error arises during code execution, the program stops running while Python passes the error to the section in the code designed to handle errors, the exception handler. If there is no exception handler in the code block, Python’s native exception-handling mechanism will take over. Python will check for error handling up the call stack. If it gets to the program’s top level without locating any exception handling, Python’s default handler will stop program execution and return a traceback — a detailed message printed to the console. It includes information about the exception type and where in the code it occurred.

Consequences of Neglecting Exception Handling

Crashing an application isn’t the worst thing that could happen if you fail to handle exceptions and edge cases during the development phase, though it is one outcome. Skipping exception handling is a security risk once the application reaches the production level. Certain errors might expose sensitive information or system vulnerabilities. Tracebacks can reveal internal logic or paths in the application that bad actors can exploit. Additionally, neglecting exception handling makes debugging more challenging, as exception handling provides more insight into where errors occur than the tracebacks.

Order of Operations

It’s a good idea to handle edge cases first. This allows you to define the expected boundaries and special conditions of your input data and processing logic. Start by defining the expected input range, limits, and unusual conditions. Once you have those parameters defined, you can implement validation and boundary checks and the logic to handle these cases appropriately.

Handling Edge Cases

The function below was designed to take in an integer parameter and return information about whether this person is a child, adult or senior. Without handling the edge cases, the function looks like this:

Without handling edge cases, the function might produce incorrect or inappropriate results, such as allowing negative ages or unrealistically high ages (e.g., 150 years old). To handle these edge cases, we can define realistic boundaries:

  • No negative numbers
  • Let’s favor on the side of optimism and cap the maximum value at 120, just in case
  • No strings or Booleans

Here’s the updated code which accounts for the edge cases:

The code should handle the edge cases first. If the execution thread reaches the functional logic, it will return the same results with the edge handling as it would without it.

Exception Handling

Python uses the try…except block to catch and handle exceptions. The basic syntax is:

The function below converts inputs into integers. This means anything that isn’t a string of numbers will error out and needs to be handled.

The console for this will display:
Conversion successful: 123
Error: Input could not be converted to an integer.

You can build multiple except clauses under a single try block. The example below divides two numbers, handling several potential issues:

  • ZeroDivisonError catches errors where b is zero.
  • TypeError catches errors where the inputs aren’t numbers.
  • Exception is a generic exception handler that catches any other unexpected errors.

Python’s Custom Exception Class

If a built-in exception handler doesn’t exist, you can create a custom one. The basic syntax is:

Most of the syntax here follows basic child class syntax but there are some elements to point out. The custom class inherits properties from the built-in exception class. The __str__ method returns a string representation of the exception. It’s often overridden to return the error message, making it easier to print or log the exception.

Let’s pretend we’re building an application that can’t accept the number 2. We can build the following exception:

In a function body, it looks like this:

Properly handling exceptions and edge cases is crucial in developing robust and secure applications. Neglecting these aspects can lead to crashes, security vulnerabilities, and challenging debugging scenarios. By implementing thorough exception and edge case handling, developers can ensure their applications run smoothly and securely, providing a better user experience and maintaining system integrity. Whether you’re dealing with built-in exceptions, edge cases, or creating custom exceptions, the goal is the same: to anticipate and manage potential issues proactively.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.