VOOZH about

URL: https://thenewstack.io/handling-edge-cases-and-exceptions-in-python/

⇱ Handling Edge Cases and Exceptions in Python - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
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%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-09-28 05:00:47
Handling Edge Cases and Exceptions in Python
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 Jessica Wachtel
👁 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.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Class.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.