Python Tutorial for Beginners 35 - Raising Exceptions In Python



hey guys welcome to the next blog on Python tutorial for beginners in the last few blogs we have seen how to handle exceptions in Python now in this blog I'm going to show you how to raise an exception in Python now raising an exception is similar to threw an error or exception in the languages like Java or C++ so Python allows us to use a keyword called raise and that raised statement or keyword allows the programmer to force a specific exception to occur so let's get started and let's see this raised statement with an example so to start with I have created a class called coffee cup which have a init method which takes one argument which is the temperature and I'm just initializing the value of temperature using this init method and this coffee cup class also have one more method which is called drink coffee and inside this drink coffee method I have some conditions so if the temperature is greater than 85 then I print coffee is too hot if the temperature is less than 65 then I print coffee is too cold and if the temperature is between 65 and 85 then I print that coffee is okay and here I have created an instance of coffee cup class with an argument which is 75 to start with and I'm just calling this method drink coffee from this object so let's run the code and let's see what happens so because the 75 is neither satisfying this condition nor this condition so the answer which is printed here is coffee okay to drink now when I provide for example 101 temperature value here and when I run the code once again it will say coffee to hot if I say coffee temperature is 10 it says coffee to cold now in these two switch patience when the coffee is too hot or the coffee is too cold I can also throw an exception and show the user some kind of message so instead of printing that coffee is too hot I can use a keyword called raise and then I can use any class of exception so we can use the exception class or any subclass of exception and you can see this exception class comes in this built-in module so I'm going to just use this exception which is the base class and same I'm going to do here when the coffee is cold and I'm going to comment these two print statements and now when I run the code it throws an exception without any error so it throws an exception and after that the execution of program stops so right now temperature is 10 which satisfies this condition so this exception is thrown and that's why we see this exception so in Python we can throw an exception using this keyword race and this exception class or any subclass of this exception class now in the last blog also I have shown you how to see all the subclasses which are under the exception class to see them you can just open your Python console and in the Python Python console you just need to import militants and then press Enter and now you use a method called help and inside the help method you use this keyword built-ins and then press Enter which is going to give you a big list here and when you scroll up at the top you will see classes here and under the classes you will see base exception and we have used this class which is exception you can see here so to raise an exception you can use this exception class or any subclass under this exception so you can use automatic error or you can use or division error buffer error whatever error is appropriate for your exception you can use from this list so right now I have just used the exception class which is the base class to throw an exception here instead of exception I can also use for example exception called value error which is also the subclass of this exception class and now when I run the code it will show me the value error so this is the one way of throwing an exception in Python now with these exceptions you can also pass an argument inside the parentheses so you can write exception and then in the parentheses you can write the message whatever you want to show when this exception is thrown so here I can just write coffee is too hot and for the another exception I am going to just say coffee to cold and when I run the code because it's a value exception which comes from this statement it shows the value error exception and then the message which we want to show so we are just showing coffee too cold so here it also prints the same message which we have given as the first argument of this exception error class so you can either raise an exception with the base class exception or the subclass of the base class exception for example value error and you can also give an optional argument here which is the message which will be displayed at the time when this exception is thrown so this is how you can raise or throw an exception in Python I hope you have enjoyed this blog and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News