ā¹ļø Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://blog.finxter.com/how-to-print-the-exception-without-exiting-your-python-program/ |
| Last Crawled | 2026-04-09 21:42:22 (2 days ago) |
| First Indexed | 2021-08-08 11:59:33 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | How to Print the Exception Without Exiting Your Python Program? - Be on the Right Side of Change |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | Problem Formulation
Given a basic Python program. How to
print
an exception if it occurs without exiting the program?
For example, consider the following program that raises a
ZeroDivisionError: division by zero
.
x = 42/0
print('Program is still running')
The output is:
Traceback (most recent call last):
File "C:\Users\xcent\Desktop\code.py", line 1, in <module>
x = 42/0
ZeroDivisionError: division by zero
You want the program to keep running and executing the print statement after giving you a note about the exception:
division by zero
Program is still running
How to accomplish this in Python?
Basic Solution: Try/Except
An exception will immediately terminate your program. To avoid this, you can
catch the exception
with a
try/except
block around the code where you expect that a certain exception may occur. Hereās how you
catch and print a given exception:
To catch and print an exception that occurred in a code snippet, wrap it in an indented
try
block, followed by the command
"except Exception as e"
that catches the exception and saves its error message in string variable
e
. You can now print the error message with
"print(e)"
or use it for further processing.
Hereās the general
exception handling framework
:
try:
# ... YOUR CODE HERE ... #
except Exception as e:
# ... PRINT THE ERROR MESSAGE ... #
print(e)
In our particular example, youād modify your program from ā¦
x = 42/0
print('Program is still running')
⦠to ā¦
try:
x = 42/0
except Exception as e:
print(e)
print('Program is still running')
Now, the output is your desired:
division by zero
Program is still running
Full Traceback Error Message
To print the full traceback of the error messageāand keep the program running without exiting on an errorāyou can use a try/except block in combination with the
traceback
moduleās
format_exc()
function.
Import the module with
import traceback
.
Print the full traceback of the error with
print(traceback.format_exc())
.
Hereās the full example code:
import traceback
import sys
try:
x = 42/0
except Exception:
print(traceback.format_exc())
print('Program is still running')
Now, the output shows the full traceback like so:
Traceback (most recent call last):
File "C:\Users\xcent\Desktop\code.py", line 5, in <module>
x = 42/0
ZeroDivisionError: division by zero
Program is still running
The last line shows that the program doesnāt
terminate
when the exception occurs.
š§āš»
Recommended
:
Python Print Exception: 13 Easy Ways to Try-Except-Print Errors for Beginners
Summary
To print an exception without exiting the program, use a try/except block and assign the exception object to variable
e
using
except Exception as e
. Now, call
print(e)
in the
except
branch to print a simple error message.
If you need a more advanced error message with full traceback, import the
traceback
module and call
print(traceback.format_exc())
in the
except
branch.
Where to Go From Here?
Enough theory. Letās get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. Thatās how you polish the skills you really need in practice. After all, whatās the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
š If your answer is
YES!
, consider becoming a
Python freelance developer
! Itās the best way of approaching the task of improving your Python skillsāeven if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar
āHow to Build Your High-Income Skill Pythonā
and learn how I grew my coding business online and how you can, tooāfrom the comfort of your own home.
Join the free webinar now! |
| Markdown | [Skip to content](https://blog.finxter.com/how-to-print-the-exception-without-exiting-your-python-program/#content "Skip to content")
[](https://blog.finxter.com/)
Menu
- [š“ YouTube](https://www.youtube.com/@finxter)
- [š Membership](https://blog.finxter.com/finxter-premium-membership/)
- [š©āš Academy](https://academy.finxter.com/)
- [š Books](https://blog.finxter.com/finxter-books/)
- [š² Puzzles](https://app.finxter.com/learn/computer/science/)
- [š¤ User Stories](https://blog.finxter.com/what-our-users-say/)
- [What Our Users Say](https://blog.finxter.com/what-our-users-say/)
- [About Finxter](https://blog.finxter.com/about/)
- [Finxter Feedback from ~1000 Python Developers](https://blog.finxter.com/finxter-feedback-from-1000-users/)
- [Video Reviews](https://blog.finxter.com/python-freelancer-course-testimonials-from-finxters/)
- [Course Review from Cristian](https://blog.finxter.com/finxter-review-become-a-freelancer-course/?tl_inbound=1&tl_target_all=1&tl_form_type=1&tl_period_type=3)
- [Course Review from Adam](https://blog.finxter.com/how-adam-earns-5000-per-month-as-a-python-freelancer-on-upwork-month-4/)
- [Review by ChatGPT](https://blog.finxter.com/what-is-finxter-is-it-trustworthy-chatgpt-answers/)
- [About Chris](https://blog.finxter.com/about-chris/)
- [Start Here ā](https://blog.finxter.com/make-money-ai/)
# How to Print the Exception Without Exiting Your Python Program?
October 11, 2023
August 8, 2021
by [Chris](https://blog.finxter.com/author/xcentpy_cfsh849y/ "View all posts by Chris")
## Problem Formulation
Given a basic Python program. How to [print](https://blog.finxter.com/python-print/ "Python print()") an exception if it occurs without exiting the program?
For example, consider the following program that raises a `ZeroDivisionError: division by zero`.
```
x = 42/0
print('Program is still running')
```
The output is:
```
Traceback (most recent call last):
File "C:\Users\xcent\Desktop\code.py", line 1, in <module>
x = 42/0
ZeroDivisionError: division by zero
```
You want the program to keep running and executing the print statement after giving you a note about the exception:
```
division by zero
Program is still running
```
How to accomplish this in Python?
## Basic Solution: Try/Except
An exception will immediately terminate your program. To avoid this, you can **catch the exception** with a `try/except` block around the code where you expect that a certain exception may occur. Hereās how you **catch and print a given exception:**
To catch and print an exception that occurred in a code snippet, wrap it in an indented `try` block, followed by the command `"except Exception as e"` that catches the exception and saves its error message in string variable `e`. You can now print the error message with `"print(e)"` or use it for further processing.
Hereās the general *exception handling framework*:
```
try:
# ... YOUR CODE HERE ... #
except Exception as e:
# ... PRINT THE ERROR MESSAGE ... #
print(e)
```
In our particular example, youād modify your program from ā¦
```
x = 42/0
print('Program is still running')
```
⦠to ā¦
```
try:
x = 42/0
except Exception as e:
print(e)
print('Program is still running')
```
Now, the output is your desired:
```
division by zero
Program is still running
```
## Full Traceback Error Message
To print the full traceback of the error messageāand keep the program running without exiting on an errorāyou can use a try/except block in combination with the `traceback` moduleās `format_exc()` function.
- Import the module with `import traceback`.
- Print the full traceback of the error with `print(traceback.format_exc())`.
Hereās the full example code:
```
import traceback
import sys
try:
x = 42/0
except Exception:
print(traceback.format_exc())
print('Program is still running')
```
Now, the output shows the full traceback like so:
```
Traceback (most recent call last):
File "C:\Users\xcent\Desktop\code.py", line 5, in <module>
x = 42/0
ZeroDivisionError: division by zero
Program is still running
```
The last line shows that the program doesnāt [terminate](https://blog.finxter.com/difference-between-exit-and-sys-exit-in-python/ "Difference Between exit() and sys.exit() in Python") when the exception occurs.
š§āš» **Recommended**: [Python Print Exception: 13 Easy Ways to Try-Except-Print Errors for Beginners](https://blog.finxter.com/how-to-catch-and-print-exception-messages-in-python/)
## Summary
To print an exception without exiting the program, use a try/except block and assign the exception object to variable `e` using `except Exception as e`. Now, call `print(e)` in the `except` branch to print a simple error message.
If you need a more advanced error message with full traceback, import the `traceback` module and call `print(traceback.format_exc())` in the `except` branch.
## Where to Go From Here?
Enough theory. Letās get some practice\!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. Thatās how you polish the skills you really need in practice. After all, whatās the use of learning theory that nobody ever needs?
**You build high-value coding skills by working on practical coding projects\!**
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
š If your answer is ***YES\!***, consider becoming a [Python freelance developer](https://blog.finxter.com/become-python-freelancer-course/)! Itās the best way of approaching the task of improving your Python skillsāeven if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar [āHow to Build Your High-Income Skill Pythonā](https://blog.finxter.com/webinar-freelancer/) and learn how I grew my coding business online and how you can, tooāfrom the comfort of your own home.
[Join the free webinar now\!](https://blog.finxter.com/webinar-freelancer/)
Categories [Error](https://blog.finxter.com/category/error/), [Exception Handling](https://blog.finxter.com/category/exception-handling/), [Python](https://blog.finxter.com/category/python/)
[How to Find the Index of an Element in a List of Lists?](https://blog.finxter.com/how-to-find-the-index-of-an-element-in-a-list-of-lists/)
[3 Little-Known NumPy Tricks in One Line \[Python Puzzle\]](https://blog.finxter.com/3-little-known-numpy-tricks-in-one-line-python-puzzle/)
**Be on the Right Side of Change** š
The world is changing exponentially. AI eliminates entire industries. 
 [12 Ways to Make Money with AI](https://blog.finxter.com/make-money-ai/) (Article)
** [Join our free email newsletter](https://blog.finxter.com/ai/) (130k subs).**
Be on the right side of change with AI and master the art of vibe coding! 
**New Finxter Tutorials:**
- [What to Code (Book): How Indie Hackers Find Million-Dollar Micro-SaaS Ideas in the Vibe Coding Era](https://blog.finxter.com/what-to-code-book-how-indie-hackers-find-million-dollar-micro-saas-ideas-in-the-vibe-coding-era/)
- [6 Best AI Book Writers (2026): Tools for Authors & Self-Publishers](https://blog.finxter.com/best-ai-book-writers/)
- [What Your Last 10 YouTube Videos Say About Your Future Business](https://blog.finxter.com/what-your-last-10-youtube-videos-say-about-your-future-business/)
- [\[Free eBook\] Automate the Boring Stuff and Make Money with Clawdbot](https://blog.finxter.com/free-ebook-automate-the-boring-stuff-and-make-money-with-clawdbot/)
- [The JSON Trick ā How to Scrape All Answers in a Subreddit? (Example Find User Needs)](https://blog.finxter.com/the-json-trick-how-to-scrape-all-answers-in-a-subreddit-example-find-user-needs/)
- [The Ghost Founder: 10 AI Startups That Can Hit \$1B Without a Single Employee](https://blog.finxter.com/the-ghost-founder-10-ai-startups-that-can-hit-1b-without-a-single-employee/)
- [Stop Testing LLMs with Poetry: Use Blackjack Instead](https://blog.finxter.com/stop-testing-llms-with-poetry-use-blackjack-instead/)
- [The āOne Niche, One Channelā rule for making your first \$1k in AI](https://blog.finxter.com/the-one-niche-one-channel-rule-for-making-your-first-1k-in-ai/)
- [Dreaming of the Mediterranean: When Will Tesla FSD Hit Europe?](https://blog.finxter.com/dreaming-of-the-mediterranean-when-will-tesla-fsd-hit-europe/)
- [DriftIDE ā A Startup Idea for 1B Vibe Coders \[Pitch Deck\]](https://blog.finxter.com/driftide-a-startup-idea-for-1b-vibe-coders-pitch-deck/)
**Finxter Categories:**
Categories
- [About](https://blog.finxter.com/about/)
- [Impressum](https://app.finxter.com/impressum)
- [Privacy](https://blog.finxter.com/privacy-policy/)
- [Terms](https://blog.finxter.com/terms-and-conditions-of-use/)
- [Puzzles](https://app.finxter.com/)
- [Academy](https://academy.finxter.com/)
- [Books & Courses](https://blog.finxter.com/)
- [Earnings Disclaimer](https://blog.finxter.com/earnings-disclaimer/)
- [Finxter Instagram](https://www.instagram.com/finxterdotcom/)
- [Finxter Twitter](https://twitter.com/FinxterDotCom)
- [Finxter Facebook](https://www.facebook.com/finxter)
- [Finxter YouTube](https://www.youtube.com/@finxter)
- [š“ YouTube](https://www.youtube.com/@finxter)
- [š Membership](https://blog.finxter.com/finxter-premium-membership/)
- [š©āš Academy](https://academy.finxter.com/)
- [š Books](https://blog.finxter.com/finxter-books/)
- [š² Puzzles](https://app.finxter.com/learn/computer/science/)
- [š¤ User Stories](https://blog.finxter.com/what-our-users-say/)
- [What Our Users Say](https://blog.finxter.com/what-our-users-say/)
- [About Finxter](https://blog.finxter.com/about/)
- [Finxter Feedback from ~1000 Python Developers](https://blog.finxter.com/finxter-feedback-from-1000-users/)
- [Video Reviews](https://blog.finxter.com/python-freelancer-course-testimonials-from-finxters/)
- [Course Review from Cristian](https://blog.finxter.com/finxter-review-become-a-freelancer-course/?tl_inbound=1&tl_target_all=1&tl_form_type=1&tl_period_type=3)
- [Course Review from Adam](https://blog.finxter.com/how-adam-earns-5000-per-month-as-a-python-freelancer-on-upwork-month-4/)
- [Review by ChatGPT](https://blog.finxter.com/what-is-finxter-is-it-trustworthy-chatgpt-answers/)
- [About Chris](https://blog.finxter.com/about-chris/)
- [Start Here ā](https://blog.finxter.com/make-money-ai/)
© 2026 Be on the Right Side of Change ⢠Built with [GeneratePress](https://generatepress.com/) |
| Readable Markdown | ## Problem Formulation
Given a basic Python program. How to [print](https://blog.finxter.com/python-print/ "Python print()") an exception if it occurs without exiting the program?
For example, consider the following program that raises a `ZeroDivisionError: division by zero`.
```
x = 42/0
print('Program is still running')
```
The output is:
```
Traceback (most recent call last):
File "C:\Users\xcent\Desktop\code.py", line 1, in <module>
x = 42/0
ZeroDivisionError: division by zero
```
You want the program to keep running and executing the print statement after giving you a note about the exception:
```
division by zero
Program is still running
```
How to accomplish this in Python?
## Basic Solution: Try/Except
An exception will immediately terminate your program. To avoid this, you can **catch the exception** with a `try/except` block around the code where you expect that a certain exception may occur. Hereās how you **catch and print a given exception:**
To catch and print an exception that occurred in a code snippet, wrap it in an indented `try` block, followed by the command `"except Exception as e"` that catches the exception and saves its error message in string variable `e`. You can now print the error message with `"print(e)"` or use it for further processing.
Hereās the general *exception handling framework*:
```
try:
# ... YOUR CODE HERE ... #
except Exception as e:
# ... PRINT THE ERROR MESSAGE ... #
print(e)
```
In our particular example, youād modify your program from ā¦
```
x = 42/0
print('Program is still running')
```
⦠to ā¦
```
try:
x = 42/0
except Exception as e:
print(e)
print('Program is still running')
```
Now, the output is your desired:
```
division by zero
Program is still running
```
## Full Traceback Error Message
To print the full traceback of the error messageāand keep the program running without exiting on an errorāyou can use a try/except block in combination with the `traceback` moduleās `format_exc()` function.
- Import the module with `import traceback`.
- Print the full traceback of the error with `print(traceback.format_exc())`.
Hereās the full example code:
```
import traceback
import sys
try:
x = 42/0
except Exception:
print(traceback.format_exc())
print('Program is still running')
```
Now, the output shows the full traceback like so:
```
Traceback (most recent call last):
File "C:\Users\xcent\Desktop\code.py", line 5, in <module>
x = 42/0
ZeroDivisionError: division by zero
Program is still running
```
The last line shows that the program doesnāt [terminate](https://blog.finxter.com/difference-between-exit-and-sys-exit-in-python/ "Difference Between exit() and sys.exit() in Python") when the exception occurs.
š§āš» **Recommended**: [Python Print Exception: 13 Easy Ways to Try-Except-Print Errors for Beginners](https://blog.finxter.com/how-to-catch-and-print-exception-messages-in-python/)
## Summary
To print an exception without exiting the program, use a try/except block and assign the exception object to variable `e` using `except Exception as e`. Now, call `print(e)` in the `except` branch to print a simple error message.
If you need a more advanced error message with full traceback, import the `traceback` module and call `print(traceback.format_exc())` in the `except` branch.
## Where to Go From Here?
Enough theory. Letās get some practice\!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. Thatās how you polish the skills you really need in practice. After all, whatās the use of learning theory that nobody ever needs?
**You build high-value coding skills by working on practical coding projects\!**
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
š If your answer is ***YES\!***, consider becoming a [Python freelance developer](https://blog.finxter.com/become-python-freelancer-course/)! Itās the best way of approaching the task of improving your Python skillsāeven if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar [āHow to Build Your High-Income Skill Pythonā](https://blog.finxter.com/webinar-freelancer/) and learn how I grew my coding business online and how you can, tooāfrom the comfort of your own home.
[Join the free webinar now\!](https://blog.finxter.com/webinar-freelancer/) |
| Shard | 121 (laksa) |
| Root Hash | 6977083233833885521 |
| Unparsed URL | com,finxter!blog,/how-to-print-the-exception-without-exiting-your-python-program/ s443 |