hey guys welcome to the next blog on Python tutorial for beginners in the last blog we have seen how to raise an exception in python using this raised keyboard and we have seen how to raise an exception using the base class which is exception or the subclasses of this exception class for example the value error or any other error which is the subclass of this exception now in this blog I'm going to show you how you can create your custom exception classes and use them to raise exception in Python so let's get started so I'm going to create a custom exception class here so let me create a class and I'm going to name it as coffee too hot exception and to create your own custom exception class you need to inherit from the exception class so you just need to provide exception as the superclass inside the parentheses and then after the colon you can just define the init method to provide the message so it takes one argument for example msg which stands for message and now what we need to do is we need to pass this message to the constructor of this exception class and how to do that you can do that using the super function so you can use super and then call the init function here which means we are calling the init method of this exception class using this super function and we just need to pass the message which comes as an argument of your custom exception class to the super class which is the exception class and that's your custom exception class now I can pass this class instead of this base exception class similarly to create the coffee too cold class I can once again write coffee to cold here and instead of using the subclass of exception I can give here my own custom exception class so let's run this program and let's see what happens so because right now the temperature which we have provided here is 10 which means this falls in this condition which says coffee too cold so we are seeing this exception which says coffee too cold and then it's going to show us this message which says coffee too cold now we have made this exception class explanatory enough so that we don't require this coffee too cold message instead of this coffee too cold message we can give some more information for the user for example we can write here coffee temperature and then we can provide the information of the coffee temperature which the user has provided so self dot underscore underscore temperature and same we can provide in the coffee too hot exception message one more thing which we need to do here is we need to convert this temperature into a string so we can use this mastered STR to convert from the integer or the float value to the string value so this is just the typecasting from the integer to the string and now we are going to run our program and see what's the result here so you can see now the result says coffee too cold exception and then it shows the temperature which is provided by the user that suggests temperature we change from ten to hundred and once again run the code now it's going to say coffee too hot and it's going to display the temperature of coffee which is hundred here so let's rewind what we have learned about creating our own custom exception class so in order to create your own custom exception class you need to inherit from this exception base class or the superclass and then you can create any custom class with any name and for passing the message which you provide as the first argument of your exception you can just create an init method which takes this message and pass it to the init method of your superclass which is the exception class so this is how you can create your own custom exception classes and use them to raise the exception in Python I hope you have enjoyed this blog and I will see you in the next blog you
Post a Comment