{"id":4497,"date":"2022-12-26T10:12:08","date_gmt":"2022-12-26T10:12:08","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=4497"},"modified":"2022-12-26T10:12:10","modified_gmt":"2022-12-26T10:12:10","slug":"python-raise-exception","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-raise-exception\/","title":{"rendered":"Python raises exception"},"content":{"rendered":"\n<p>In this article, we are going to learn about how can we raise exceptions in Python. The concept of exception handling is very important and useful in programming, and in Python as well, we can do exception handling. First of all, we would understand what is an exception, and then we would understand how can we raise some exceptions, and how is it useful for us.<\/p>\n\n\n\n<h2 class=\"has-text-align-center wp-block-heading\">Python raises exception<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/12\/Python-raise-exception.jpg\" alt=\"\" class=\"wp-image-4498\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/12\/Python-raise-exception.jpg 640w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/12\/Python-raise-exception-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">What is an Exception?<\/h3>\n\n\n\n<p>Well, an exception can be defined as some event, which disturbs the normal flow of our program. For example, let&#8217;s say that you are trying to open some file to read data from it in the program, and the file is not at the specified location, so this is an exception FileNotFoundError. If otherwise, the file was there, it would have been able to do the operations, but since the file was not found, it did not know what to do, and it just terminated abnormally.<\/p>\n\n\n\n<p>So, in python, we can handle the exception. For that, we just write the critical instructions within the try block, and we have the corresponding except for blocks for exceptions if they occur.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the need to raise exceptions?<\/h3>\n\n\n\n<p>In our Python programs, there are situations, when some exception is thrown and we need to deal with it. But at times, we might need to raise some exceptions ourselves. For example, let&#8217;s say that you are writing a program, and in that, you are just accepting a number greater than or equal to zero as user input, and if the user enters some number that is less than zero, then you want to raise an exception. In such cases, we would want to raise the exception ourselves, and here comes the raise keyword to our rescue. <\/p>\n\n\n\n<p>Also, if we are creating our custom exceptions, then also, we would need to raise them whenever and wherever required. So, in such situations as well, we can make use of the raise keyword to raise the exception.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How to raise exceptions?<\/h4>\n\n\n\n<p>Now, that we have a brief idea about exceptions, and the need for raising exceptions ourselves, let&#8217;s have a look at a simple python program, through which, we can understand the use of the raise keyword, and also understand how can we raise exceptions in our python programs.<\/p>\n\n\n\n<p>Program example to raise an exception &#8211;<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">number = int(input(&#8220;Please enter a number greater than or equal to 0: &#8220;))<br>if number &lt; 0:<br>\u00a0 \u00a0 raise Exception(&#8220;Sorry&#8230; no negative numbers allowed here. &#8220;)<br>else:<br>\u00a0 \u00a0 print(f&#8221;You entered the number: {number}&#8221;)<\/p>\n\n\n\n<p>As you can see in the above program, we are just taking some number as user input, and then we are just checking if the number is less than zero, and if the condition is True, then we are raising an Exception, with some message. Otherwise, we are just doing normal things.<\/p>\n\n\n\n<p>So, the thing is that whenever you want to raise some exception by yourselves, then you can simply use the raise keyword, and raise the required exception.<\/p>\n\n\n\n<p>Note that we are raising an exception here, and the exception type should be the subclass of BaseException.<\/p>\n\n\n\n<p>Let&#8217;s consider another example, just for more clarity. You might be familiar with the ATM machine. Let&#8217;s say that we have an ATM machine, from which, we can withdraw the amount, which is multiple of 500. so, if the user enters some amount that violates this rule, then we might not want to continue the transaction and exit. Let&#8217;s have a look at a simple and dummy demonstration for the same &#8211;<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">amount = int(input(&#8220;Enter The amount: (only multiples of 500 are allowed): &#8220;))<br>if amount%500 == 0:<br>\u00a0 \u00a0 print(&#8220;You can proceed with the withdrawal!&#8221;)<br>else:<br>\u00a0 \u00a0 raise ValueError(&#8220;Sorry&#8230; you entered an invalid amount!&#8221;)<\/p>\n\n\n\n<p>As you can see in the above simple program, we are just getting the amount as user input, and then we are just checking if the amount is a multiple of 500. If the condition is True, then we can just proceed with the further transaction, and if not, we are going to raise an exception with some message. <\/p>\n\n\n\n<p>You can try to execute the above program and observe the output. The thing is that the above programs will terminate abnormally when the exception is raised since we are not handling the exception, but raising it.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion:<\/h4>\n\n\n\n<p>In this article, we have understood how can we raise some exceptions in our python programs. We have made use of the <strong>raise<\/strong> keyword in Python, to raise some exceptions.&nbsp;<\/p>\n\n\n\n<p>We can understand that we can make use of the raise keyword, to raise some exceptions in our Python program. Also, with the above programs, you can understand the syntax for using the raise keyword, to raise some exceptions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">FAQ About Python raise an exception<\/h4>\n\n\n\n<div class=\"wp-block-rank-math-faq-block\"><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: What is an exception?<\/h3><div class=\"rank-math-answer\"><strong>Ans: <\/strong>Exception can be considered as some event, which occurs during the execution of the program, which disrupts the normal flow of the program execution.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: How can we raise some exceptions in python?<\/h3><div class=\"rank-math-answer\"><strong>Ans: <\/strong>We can make use of the raise keyword, to raise some exceptions in Python.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: What is exception handling in Python?<\/h3><div class=\"rank-math-answer\"><strong>Ans: <\/strong>At times, in our python program, we might face some exceptions in our programs when executing, resulting in abnormal termination of the program. The thing is that we can handle the exceptions. Python gives us the power to handle the exception. We just need to write the critical instructions within the try block, and for the exceptions that might occur in the program, we write the except blocks.<\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to learn about how can we raise exceptions in Python. The concept of exception handling is very important and useful in programming, and in Python as well, we can do exception handling. First of all, we would understand what is an exception, and then we would understand how can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4498,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[26,89],"class_list":{"0":"post-4497","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python","8":"tag-python","9":"tag-python-raise-exception"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=4497"}],"version-history":[{"count":1,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4497\/revisions"}],"predecessor-version":[{"id":4499,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4497\/revisions\/4499"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media\/4498"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=4497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=4497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=4497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}