{"id":3101,"date":"2021-11-28T04:13:21","date_gmt":"2021-11-28T04:13:21","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3101"},"modified":"2025-03-28T03:55:39","modified_gmt":"2025-03-28T03:55:39","slug":"python-custom-exception","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-custom-exception\/","title":{"rendered":"Python Custom Exception"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to define Python custom exception classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-custom-exception'>Introduction to the Python custom exception <a href=\"#introduction-to-the-python-custom-exception\" class=\"anchor\" id=\"introduction-to-the-python-custom-exception\" title=\"Anchor for Introduction to the Python custom exception\">#<\/a><\/h2>\n\n\n\n<p>To create a custom <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-exceptions\/\">exception<\/a> class, you <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">define a class<\/a> that <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-inheritance\/\">inherits<\/a> from the built-in <code>Exception<\/code> class or one of its subclasses such as <code>ValueError<\/code> class:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/11\/python-custom-exception.svg\" alt=\"Python Custom Exception\" class=\"wp-image-3103\"\/><\/figure><\/div>\n\n\n\n<p>The following example defines a <code>CustomException<\/code> class that inherits from the <code>Exception<\/code> class:<\/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-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">CustomException<\/span><span class=\"hljs-params\">(Exception)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\" my custom exception class \"\"\"<\/span><\/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 class=\"note\">Note that the <code>CustomException<\/code> class has a docstring that behaves like a statement. Therefore, you don&#8217;t need to add the <code>pass<\/code> statement to make the syntax valid.<\/p>\n\n\n\n<p>To raise the CustomException, you use the <code><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-raise-exception\/\">raise<\/a><\/code> statement. For example, the following uses the <code>raise<\/code> statement to raise the <code>CustomException<\/code>:<\/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-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">CustomException<\/span><span class=\"hljs-params\">(Exception)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\" my custom exception class \"\"\"<\/span>\n\n\n<span class=\"hljs-keyword\">try<\/span>:\n    <span class=\"hljs-keyword\">raise<\/span> CustomException(<span class=\"hljs-string\">'This is my custom exception'<\/span>)\n<span class=\"hljs-keyword\">except<\/span> CustomException <span class=\"hljs-keyword\">as<\/span> ex:\n    print(ex)<\/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=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">This <span class=\"hljs-keyword\">is<\/span> my custom exception<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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>Like standard exception classes, custom exceptions are also classes. Hence, you can add functionality to the custom exception classes like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Adding attributes and <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-properties\/\">properties<\/a>.<\/li><li>Adding <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-methods\/\">methods<\/a> e.g., log the exception, format the output, etc.<\/li><li>Overriding the <code>__str__<\/code> and <code>__repr__<\/code> methods<\/li><li>And doing anything else that you can do with regular classes.<\/li><\/ul>\n\n\n\n<p>In practice, you&#8217;ll want to keep the custom exceptions organized by creating a custom exception hierarchy. The custom exception hierarchy allows you to catch exceptions at multiple levels, like the standard exception classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-custom-exception-example'>Python custom exception example <a href=\"#python-custom-exception-example\" class=\"anchor\" id=\"python-custom-exception-example\" title=\"Anchor for Python custom exception example\">#<\/a><\/h2>\n\n\n\n<p>Suppose you need to develop a program that converts a temperature from Fahrenheit to Celsius.<\/p>\n\n\n\n<p>The minimum and maximum values of a temperature in Fahrenheit are 32 and 212. If users enter a value that is not in this range, you want to raise a custom exception e.g., <code>FahrenheitError<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='define-the-fahrenheiterror-custom-exception-class'>Define the FahrenheitError custom exception class <a href=\"#define-the-fahrenheiterror-custom-exception-class\" class=\"anchor\" id=\"define-the-fahrenheiterror-custom-exception-class\" title=\"Anchor for Define the FahrenheitError custom exception class\">#<\/a><\/h3>\n\n\n\n<p>The following defines the <code>FahrenheitError<\/code> exception class:<\/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-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">FahrenheitError<\/span><span class=\"hljs-params\">(Exception)<\/span>:<\/span>\n    min_f = <span class=\"hljs-number\">32<\/span>\n    max_f = <span class=\"hljs-number\">212<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, f, *args)<\/span>:<\/span>\n        super().__init__(args)\n        self.f = f\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__str__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'The <span class=\"hljs-subst\">{self.f}<\/span> is not in a valid range <span class=\"hljs-subst\">{self.min_f, self.max_f}<\/span>'<\/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>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, define the  FahrenheitError  class that inherits from the <code>Exception<\/code> class.<\/li><li>Second, add two class attributes <code>min_f<\/code> and <code>max_f<\/code> that represent the minimum and maximum Fahrenheit values.<\/li><li>Third, define the <code><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__init__\/\">__init__<\/a><\/code> method that accepts a Fahrenheit value (<code>f<\/code>) and a number of position arguments (<code>*args<\/code>). In the <code>__init__<\/code> method, call the <code>__init__<\/code> method of the base class. Also, assign the <code>f<\/code> argument to the <code>f<\/code> instance attribute.<\/li><li>Finally, override the <code><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__str__\/\">__str__<\/a><\/code> method to return a custom string representation of the class instance.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='define-the-fahrenheit_to_celsius-function'>Define the fahrenheit_to_celsius function <a href=\"#define-the-fahrenheit_to_celsius-function\" class=\"anchor\" id=\"define-the-fahrenheit_to_celsius-function\" title=\"Anchor for Define the fahrenheit_to_celsius function\">#<\/a><\/h3>\n\n\n\n<p>The following defines the <code>fahrenheit_to_celsius<\/code> function that accepts a temperature in Fahrenheit and returns a temperature in Celcius:<\/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\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fahrenheit_to_celsius<\/span><span class=\"hljs-params\">(f: float)<\/span> -&gt; float:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> f &lt; FahrenheitError.min_f <span class=\"hljs-keyword\">or<\/span> f &gt; FahrenheitError.max_f:\n        <span class=\"hljs-keyword\">raise<\/span> FahrenheitError(f)\n\n    <span class=\"hljs-keyword\">return<\/span> (f - <span class=\"hljs-number\">32<\/span>) * <span class=\"hljs-number\">5<\/span> \/ <span class=\"hljs-number\">9<\/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>The <code>fahrenheit_to_celsius<\/code> function raises the <code>FahrenheitError<\/code> excpetion if the input temperature is not in the valid range. Otherwise, it converts the temperature from Fahrenheit to Celcius.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='create-the-main-program'>Create the main program <a href=\"#create-the-main-program\" class=\"anchor\" id=\"create-the-main-program\" title=\"Anchor for Create the main program\">#<\/a><\/h3>\n\n\n\n<p>The following main program uses the <code>fahrenheit_to_celsius<\/code> function and the <code>FahrenheitError<\/code> custom exception class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    f = input(<span class=\"hljs-string\">'Enter a temperature in Fahrenheit:'<\/span>)\n    <span class=\"hljs-keyword\">try<\/span>:\n        f = float(f)\n    <span class=\"hljs-keyword\">except<\/span> ValueError <span class=\"hljs-keyword\">as<\/span> ex:\n        print(ex)\n    <span class=\"hljs-keyword\">else<\/span>:\n        <span class=\"hljs-keyword\">try<\/span>:\n            c = fahrenheit_to_celsius(float(f))\n        <span class=\"hljs-keyword\">except<\/span> FahrenheitError <span class=\"hljs-keyword\">as<\/span> ex:\n            print(ex)\n        <span class=\"hljs-keyword\">else<\/span>:\n            print(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{f}<\/span> Fahrenheit = <span class=\"hljs-subst\">{c:<span class=\"hljs-number\">.4<\/span>f}<\/span> Celsius'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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>How it works.<\/p>\n\n\n\n<p>First, prompt users for a temperature in Fahrenheit.<\/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\">f = input(<span class=\"hljs-string\">'Enter a temperature in Fahrenheit:'<\/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>Second, convert the input value into a <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float\/\">float<\/a>. If the <code>float()<\/code> cannot convert the input value, the program will raise a <code>ValueError<\/code> exception. In this case, it displays the error message from the <code>ValueError<\/code> exception:<\/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\"><span class=\"hljs-keyword\">try<\/span>:\n    f = float(f)\n    <span class=\"hljs-comment\"># ...<\/span>\n<span class=\"hljs-keyword\">except<\/span> ValueError <span class=\"hljs-keyword\">as<\/span> ex:\n    print(ex)<\/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>Third, convert the temperature to Celsius by calling the <code>fahrenheit_to_celsius<\/code> function and print the error message if the input value is not a valid <code>Fahrenheit<\/code> value:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">try<\/span>:\n    c = fahrenheit_to_celsius(float(f))\n<span class=\"hljs-keyword\">except<\/span> FahrenheitError <span class=\"hljs-keyword\">as<\/span> ex:\n    print(ex)\n<span class=\"hljs-keyword\">else<\/span>:\n    print(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{f}<\/span> Fahrenheit = <span class=\"hljs-subst\">{c:<span class=\"hljs-number\">.4<\/span>f}<\/span> Celsius'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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<h3 class=\"wp-block-heading\" id='put-it-all-together'>Put it all together <a href=\"#put-it-all-together\" class=\"anchor\" id=\"put-it-all-together\" title=\"Anchor for Put it all together\">#<\/a><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">FahrenheitError<\/span><span class=\"hljs-params\">(Exception)<\/span>:<\/span>\n    min_f = <span class=\"hljs-number\">32<\/span>\n    max_f = <span class=\"hljs-number\">212<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, f, *args)<\/span>:<\/span>\n        super().__init__(args)\n        self.f = f\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__str__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'The <span class=\"hljs-subst\">{self.f}<\/span> is not in a valid range <span class=\"hljs-subst\">{self.min_f, self.max_f}<\/span>'<\/span>\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fahrenheit_to_celsius<\/span><span class=\"hljs-params\">(f: float)<\/span> -&gt; float:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> f &lt; FahrenheitError.min_f <span class=\"hljs-keyword\">or<\/span> f &gt; FahrenheitError.max_f:\n        <span class=\"hljs-keyword\">raise<\/span> FahrenheitError(f)\n\n    <span class=\"hljs-keyword\">return<\/span> (f - <span class=\"hljs-number\">32<\/span>) * <span class=\"hljs-number\">5<\/span> \/ <span class=\"hljs-number\">9<\/span>\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    f = input(<span class=\"hljs-string\">'Enter a temperature in Fahrenheit:'<\/span>)\n    <span class=\"hljs-keyword\">try<\/span>:\n        f = float(f)\n    <span class=\"hljs-keyword\">except<\/span> ValueError <span class=\"hljs-keyword\">as<\/span> ex:\n        print(ex)\n    <span class=\"hljs-keyword\">else<\/span>:\n        <span class=\"hljs-keyword\">try<\/span>:\n            c = fahrenheit_to_celsius(float(f))\n        <span class=\"hljs-keyword\">except<\/span> FahrenheitError <span class=\"hljs-keyword\">as<\/span> ex:\n            print(ex)\n        <span class=\"hljs-keyword\">else<\/span>:\n            print(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{f}<\/span> Fahrenheit = <span class=\"hljs-subst\">{c:<span class=\"hljs-number\">.4<\/span>f}<\/span> Celsius'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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<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>Subclass the <code>Exception<\/code> class or one of its subclasses to define a custom exception class.<\/li><li>Create a exception class hierarchy to make the exception classes more organized and catch exceptions at multiple levels.<\/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=\"3101\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-custom-exception\/\"\n\t\t\t\tdata-post-title=\"Python Custom 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=\"3101\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-custom-exception\/\"\n\t\t\t\tdata-post-title=\"Python Custom 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>Summary: in this tutorial, you&#8217;ll learn how to define Python custom exception classes. Introduction to the Python custom exception # To create a custom exception class, you define a class that inherits from the built-in Exception class or one of its subclasses such as ValueError class: The following example defines a CustomException class that inherits [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":49,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3101","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3101","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=3101"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3101\/revisions"}],"predecessor-version":[{"id":7187,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3101\/revisions\/7187"}],"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=3101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}