{"id":3076,"date":"2021-11-27T09:57:50","date_gmt":"2021-11-27T09:57:50","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3076"},"modified":"2021-11-27T10:07:59","modified_gmt":"2021-11-27T10:07:59","slug":"python-raise-exception","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-raise-exception\/","title":{"rendered":"Python Raise Exception"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to raise exceptions by using the Python <code>raise<\/code> statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-raise-statement'>Introduction to the Python raise statement <a href=\"#introduction-to-the-python-raise-statement\" class=\"anchor\" id=\"introduction-to-the-python-raise-statement\" title=\"Anchor for Introduction to the Python raise statement\">#<\/a><\/h2>\n\n\n\n<p>To raise an <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-exceptions\/\">exception<\/a>, you use the <code>raise<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">raise<\/span> ExceptionType()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>ExceptionType()<\/code> must be subclass of the <code>BaseException<\/code> class. Typically, it is a subclass of the <code>Exception<\/code> class. Note that the <code>ExceptionType<\/code> doesn&#8217;t need to be directly inherited from the <code>Exception<\/code> class. It can indirectly inherit from a class that is a subclass of the Exception class.<\/p>\n\n\n\n<p>The <code>BaseException<\/code> class has the <code>__init__<\/code> method that accepts an <code>*args<\/code> argument. It means that you can pass any number of arguments to the exception object when raising an exception.<\/p>\n\n\n\n<p>The following example uses the <code>raise<\/code> statement to raise a <code>ValueError<\/code> exception. It passes three arguments to the <code>ValueError<\/code> <code>__init__<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">try<\/span>:\n    <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The value error exception'<\/span>, <span class=\"hljs-string\">'x'<\/span>, <span class=\"hljs-string\">'y'<\/span>)\n<span class=\"hljs-keyword\">except<\/span> ValueError <span class=\"hljs-keyword\">as<\/span> ex:\n    print(ex.args)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/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-3\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">('The value error exception', 'x', 'y')<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='reraise-the-current-exception'>Reraise the current exception <a href=\"#reraise-the-current-exception\" class=\"anchor\" id=\"reraise-the-current-exception\" title=\"Anchor for Reraise the current exception\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you want to log an exception and raise the same exception again. In this case, you can use the <code>raise<\/code> statement without specifying the exception object. <\/p>\n\n\n\n<p>For example, the following defines a <code>division()<\/code> function that returns the division of two numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">division<\/span><span class=\"hljs-params\">(a, b)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-keyword\">return<\/span> a \/ b\n    <span class=\"hljs-keyword\">except<\/span> ZeroDivisionError <span class=\"hljs-keyword\">as<\/span> ex:\n        print(<span class=\"hljs-string\">'Logging exception:'<\/span>, str(ex))\n        <span class=\"hljs-keyword\">raise<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you pass zero to the second argument of the <code>division()<\/code> function, the <code>ZeroDivisionError<\/code> exception will occur. However, instead of handling the exception, you can log the exception and raise it again.<\/p>\n\n\n\n<p>Note that you don&#8217;t need to specify the exception object in the <code>raise<\/code> statement. In this case, Python knows that the <code>raise<\/code> statement will raise the current exception that has been caught by the <code>except<\/code> clause.<\/p>\n\n\n\n<p>The following code causes a <code>ZeroDivisionError<\/code> exception:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">division(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">0<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And you&#8217;ll see both the logging message and the exception in the output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Logging exception: division by zero\nTraceback (most recent call last):\n  File \"c:\/pythontutorial\/app.py\", line 9, in &lt;module&gt;\n    division(1, 0)\n  File \"C:\/pythontutorial\/app.py\", line 3, in division\n    return a \/ b\nZeroDivisionError: division by zero<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='raise-another-exception-during-handling-an-exception'>Raise another exception during handling an exception <a href=\"#raise-another-exception-during-handling-an-exception\" class=\"anchor\" id=\"raise-another-exception-during-handling-an-exception\" title=\"Anchor for Raise another exception during handling an exception\">#<\/a><\/h2>\n\n\n\n<p>When handling an exception, you may want to raise another exception. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">division<\/span><span class=\"hljs-params\">(a, b)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-keyword\">return<\/span> a \/ b\n    <span class=\"hljs-keyword\">except<\/span> ZeroDivisionError <span class=\"hljs-keyword\">as<\/span> ex:\n        <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'b must not zero'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>division()<\/code> function, we raise a <code>ValueError<\/code>exception if the <code>ZeroDivisionError<\/code> occurs.<\/p>\n\n\n\n<p>If you run the following code, you&#8217;ll get the detail of the stack trace:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">division(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">0<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/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-9\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Traceback (most recent call last):\n  File \"C:\/pythontutorial\/app.py\", line 3, in division\n    return a \/ b\nZeroDivisionError: division by zero\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"C:\/pythontutorial\/app.py\", line 8, in &lt;module&gt;\n    division(1, 0)\n  File \"C:\/pythontutorial\/app.py\", line 5, in division\n    raise ValueError('b must not zero')\nValueError: b must not zero<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>First, the <code>ZeroDivisionError<\/code> exception occurs:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Traceback (most recent call last):\n  File \"C:\/pythontutorial\/app.py\", line 3, in division\n    return a \/ b\nZeroDivisionError: division by zero<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, during handling the <code>ZeroDivisionError<\/code> exception, the <code>ValueError<\/code> exception occurs:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Traceback (most recent call last):\n  File \"C:\/pythontutorial\/app.py\", line 8, in &lt;module&gt;\n    division(1, 0)\n  File \"C:\/pythontutorial\/app.py\", line 5, in division\n    raise ValueError('b must not zero')\nValueError: b must not zero<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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\"><li>Use the Python <code>raise<\/code> statement to raise an exception.<\/li><li>When handling exception, you can raise the same or another exception.<\/li><\/ul>\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=\"3076\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-raise-exception\/\"\n\t\t\t\tdata-post-title=\"Python Raise Exception\"\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=\"3076\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-raise-exception\/\"\n\t\t\t\tdata-post-title=\"Python Raise Exception\"\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 how to raise exceptions by using the Python raise statement<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":47,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3076","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3076","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=3076"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3076\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=3076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}