{"id":884,"date":"2020-10-29T06:36:47","date_gmt":"2020-10-29T06:36:47","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=884"},"modified":"2025-03-30T10:26:52","modified_gmt":"2025-03-30T10:26:52","slug":"python-try-except-finally","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-try-except-finally\/","title":{"rendered":"Python try&#8230;except&#8230;finally"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python <code>try...except...finally<\/code> statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-try-except-finally-statement'>Introduction to Python try&#8230;except&#8230;finally statement <a href=\"#introduction-to-python-try-except-finally-statement\" class=\"anchor\" id=\"introduction-to-python-try-except-finally-statement\" title=\"Anchor for Introduction to Python try...except...finally statement\">#<\/a><\/h2>\n\n\n\n<p>The <code>try...except<\/code> statement allows you to <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-try-except\/\">catch one or more exceptions<\/a> in the <code>try<\/code> clause and handle each of them in the <code>except<\/code> clauses.<\/p>\n\n\n\n<p>The <code>try...except<\/code> statement also has an optional clause called <code>finally<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">try<\/span>:\n    <span class=\"hljs-comment\"># code that may cause exceptions<\/span>\nexcept:\n    <span class=\"hljs-comment\"># code that handle exceptions<\/span>\n<span class=\"hljs-keyword\">finally<\/span>:\n    <span class=\"hljs-comment\"># code that clean up<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>finally<\/code> clause always executes whether an exception occurs or not. And it executes after the <code>try<\/code> clause and any <code>except<\/code> clause.<\/p>\n\n\n\n<p>The following flowchart illustrates the <code>try...except...finally<\/code> clause:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"353\" height=\"506\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/try-except-finally.png\" alt=\"Python try catch finally statement\" class=\"wp-image-885\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/try-except-finally.png 353w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/try-except-finally-209x300.png 209w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id='python-try-except-finally-statement-examples'>Python try&#8230;except&#8230;finally statement examples <a href=\"#python-try-except-finally-statement-examples\" class=\"anchor\" id=\"python-try-except-finally-statement-examples\" title=\"Anchor for Python try...except...finally statement examples\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>try...except...finally<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">a = <span class=\"hljs-number\">10<\/span>\nb = <span class=\"hljs-number\">0<\/span>\n\n<span class=\"hljs-keyword\">try<\/span>:\n    c = a \/ b\n    <span class=\"hljs-keyword\">print<\/span>(c)\nexcept ZeroDivisionError <span class=\"hljs-keyword\">as<\/span> error:\n    <span class=\"hljs-keyword\">print<\/span>(error)\n<span class=\"hljs-keyword\">finally<\/span>:\n    <span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">'Finishing up.'<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">division by zero\nFinishing up.<\/code><\/span><\/pre>\n\n\n<p>In this example, the <code>try<\/code> clause causes a <code>ZeroDivisionError<\/code> exception both <code>except<\/code> and <code>finally<\/code> clause executes.<\/p>\n\n\n\n<p>The <code>try<\/code> clause in the following example doesn&#8217;t cause an error. Therefore, all statements in the <code>try<\/code> and <code>finally<\/code> clauses execute:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">a = <span class=\"hljs-number\">10<\/span>\nb = <span class=\"hljs-number\">2<\/span>\n\n<span class=\"hljs-keyword\">try<\/span>:\n    c = a \/ b\n    <span class=\"hljs-keyword\">print<\/span>(c)\nexcept ZeroDivisionError <span class=\"hljs-keyword\">as<\/span> error:\n    <span class=\"hljs-keyword\">print<\/span>(error)\n<span class=\"hljs-keyword\">finally<\/span>:\n    <span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">'Finishing up.'<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">5<span class=\"hljs-selector-class\">.0<\/span>\n<span class=\"hljs-selector-tag\">Finishing<\/span> <span class=\"hljs-selector-tag\">up<\/span>.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-try-finally-statement'>Python try&#8230;finally statement <a href=\"#python-try-finally-statement\" class=\"anchor\" id=\"python-try-finally-statement\" title=\"Anchor for Python try...finally statement\">#<\/a><\/h2>\n\n\n\n<p>The <code>catch<\/code> clause in the <code>try...except...finally<\/code> statement is optional. So you can write it like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">try<\/span>:\n    <span class=\"hljs-comment\"># the code that may cause an exception<\/span>\n<span class=\"hljs-keyword\">finally<\/span>:\n    <span class=\"hljs-comment\"># the code that always executes<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Typically, you use this statement when you cannot handle the exception but you want to clean up resources. For example, you want to close the file that has been opened.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use Python <code>try...except...finally<\/code> statement to execute a code block whether an exception occurs or not.<\/li>\n\n\n\n<li>Use the <code>finally<\/code> clause to clean up the resources such as closing files.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=try-except-finally\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"884\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-try-except-finally\/\"\n\t\t\t\tdata-post-title=\"Python try&#8230;except&#8230;finally\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"884\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-try-except-finally\/\"\n\t\t\t\tdata-post-title=\"Python try&#8230;except&#8230;finally\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Python try&#8230;except&#8230;finally statement.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":63,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-884","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=884"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/884\/revisions"}],"predecessor-version":[{"id":7222,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/884\/revisions\/7222"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}