Title: Python Exceptions: An Introduction – Real Python
Open Graph Title: Python Exceptions: An Introduction – Real Python
Description: In this beginner tutorial, you'll learn what exceptions are good for in Python. You'll see how to raise exceptions and how to handle them with try ... except blocks.
Open Graph Description: In this beginner tutorial, you'll learn what exceptions are good for in Python. You'll see how to raise exceptions and how to handle them with try ... except blocks.
Mail addresses
?subject=Python article for you&body=Python Exceptions: An Introduction on Real Python
https://realpython.com/python-exceptions/
Opengraph URL: https://realpython.com/python-exceptions/
X: @realpython
Domain: realpython.com
{
"@context": "http://schema.org",
"@type": "Article",
"headline": "Python Exceptions: An Introduction",
"image": {
"@type": "ImageObject",
"url": "https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg",
"width": 1920,
"height": 1080
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://realpython.com/python-exceptions/",
"lastReviewed": "2024-12-01",
"author": {
"@type": "Person",
"name": "Said van de Klundert",
"image": "https://realpython.com/cdn-cgi/image/width=335,height=335,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/AAEAAQAAAAAAAAQiAAAAJGJmOGNlMzMzLTg1MmEtNGQwYy1hZDkyLTEwYzI0MjRjNTZkOA_2.8aa7cc5bf1f9.jpg",
"url": "https://realpython.com/team/svdklundert/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"reviewedBy": [
{
"@type": "Person",
"name": "Adriana Cutenco",
"image": "https://realpython.com/cdn-cgi/image/width=900,height=900,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/acutenco.676e4197c133.jpg",
"url": "https://realpython.com/team/acutenco/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Brenda Weleschuk",
"image": "https://realpython.com/cdn-cgi/image/width=320,height=320,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/IMG_3324_1.50b309355fc1.jpg",
"url": "https://realpython.com/team/bweleschuk/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Bartosz Zaczy\u0144ski",
"image": "https://realpython.com/cdn-cgi/image/width=1694,height=1694,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/coders_lab_2109368.259b1599fbee.jpg",
"url": "https://realpython.com/team/bzaczynski/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Geir Arne Hjelle",
"image": "https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/gahjelle.470149ee709e.jpg",
"url": "https://realpython.com/team/gahjelle/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Joanna Jablonski",
"image": "https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/jjablonksi-avatar.e37c4f83308e.jpg",
"url": "https://realpython.com/team/jjablonski/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Kate Finegan",
"image": "https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/VZxEtUor_400x400.7169c68e3950.jpg",
"url": "https://realpython.com/team/kfinegan/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Martin Breuss",
"image": "https://realpython.com/cdn-cgi/image/width=456,height=456,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/martin_breuss_python_square.efb2b07faf9f.jpg",
"url": "https://realpython.com/team/mbreuss/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
}
]
},
"datePublished": "2024-12-01T14:00:00+00:00",
"dateModified": "2024-12-01T14:09:26.410718+00:00",
"publisher": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": {
"@type": "ImageObject",
"url": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png",
"width": 512,
"height": 512
},
"description": "Real Python is a leading provider of online Python education and one of the largest language-specific online communities for software developers. It publishes high-quality learning resources, such as tutorials, books, and courses to an audience of millions of developers, data scientists, and machine learning engineers each month.",
"slogan": "Become a Python Expert",
"email": "info@realpython.com",
"sameAs": [
"https://github.com/realpython",
"https://www.youtube.com/realpython",
"https://twitter.com/realpython",
"https://x.com/realpython",
"https://www.linkedin.com/company/realpython-com/",
"https://www.facebook.com/learnrealpython",
"https://www.instagram.com/realpython",
"https://www.tiktok.com/@realpython.com"
]
},
"author": {
"@type": "Person",
"name": "Said van de Klundert",
"image": "https://realpython.com/cdn-cgi/image/width=335,height=335,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/AAEAAQAAAAAAAAQiAAAAJGJmOGNlMzMzLTg1MmEtNGQwYy1hZDkyLTEwYzI0MjRjNTZkOA_2.8aa7cc5bf1f9.jpg",
"url": "https://realpython.com/team/svdklundert/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"description": "In this beginner tutorial, you'll learn what exceptions are good for in Python. You'll see how to raise exceptions and how to handle them with try ... except blocks.",
"hasPart": {
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What are exceptions in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Exceptions in Python are errors that occur during the execution of a program, disrupting the normal flow of the program.
"
}
},
{
"@type": "Question",
"name": "How are exceptions handled in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You handle exceptions in Python using a try … except block. Python executes the code in the try block and if an exception occurs, it switches to executing the code in the except block to handle the exception. However, only the exceptions that are explicitly specified in the except block will be handled. If an exception is not caught, it’ll propagate up the call stack and may result in the termination of your program.
"
}
},
{
"@type": "Question",
"name": "How do you catch all exceptions in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "To catch all exceptions in Python, you can use a bare except clause or write except Exception, but it’s recommended to catch specific exceptions to avoid masking unexpected errors.
"
}
},
{
"@type": "Question",
"name": "How does try \u2026 except in Python work?",
"acceptedAnswer": {
"@type": "Answer",
"text": "In a try … except block, Python executes the code under try and if an exception occurs, it immediately jumps to the except block to handle it, allowing the program to continue running.
"
}
},
{
"@type": "Question",
"name": "What does try \u2026 except pass do in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Using try … except with pass allows the program to ignore the exception and continue execution without taking any specific action in response to the error. However, this practice can hide potential issues, making it harder to debug and maintain the code, so use it with caution. It’s generally better to either handle the exception explicitly or log it for debugging purposes.
"
}
},
{
"@type": "Question",
"name": "How do you raise an exception in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You raise an exception in Python using the raise keyword followed by an exception object, which can include a custom message.
"
}
},
{
"@type": "Question",
"name": "What is the purpose of using assert in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You can use the assert keyword to check if a condition is true during development. If the condition is false, it raises an AssertionError, which can help with debugging. Note that assertions can be disabled by running Python with the -O (optimize) flag. Therefore, you shouldn’t rely on assertions for critical checks in production code, as they may be ignored.
"
}
},
{
"@type": "Question",
"name": "What is the role of the finally clause in exception handling?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The finally clause contains code that will always execute after a try block, regardless of whether an exception was raised or not, ensuring necessary cleanup actions occur.
"
}
}
]
}
}
| author | Real Python |
| twitter:card | summary_large_image |
| twitter:image | https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg |
| og:image | https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg |
| twitter:creator | @realpython |
| og:type | article |
Links:
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover
Robots: max-image-preview:large