35 How to Raise Exception in Python programming?

Опубликовано: 19 Октябрь 2024
на канале: Free Tutorial TV
6
1

now raising an exception is similar to throw an error or exception in the languages like Java or

C++.

So Python allows us to use our keyword called raise. and that raise statement or keyword allows the programmer

to force a specific exception to occur.

So let's get started and let's see this raise statement with an example.

So to start with I have created a class called coffee cup . which have init Method which takes one argument.

which is that temperature. and I'm just initializing the value of temperature using this init Method.

And this coffee cup class also have one more matter method 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 pretend that coffee is ok.

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 that coffee is OK to drink.

Now when I provide for example a hundred and one temperature value here. and when I run the code once

again it will say coffee too hot.

If I say coffee temperature is 10 it says coffee too cold.

Now in these two situations 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 creating 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 the same.

I'm going to do here when the coffee is cold . and I'm going to command these to 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 raise and this exception class or any subclass

of this exception class .and in the last video.

Also I've 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 console.

You just need to import built ins and then press enter.

And now you use a method call the help and inside help Method.

You use this keyword built ins and then press enter.

We're just 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.

**************************************************************************
please like and follow us in Facebook and Instagram.
facebook:   / technologytv  


Instagram:   / heikhamsingh  


Twitter:   / hanandkumarsing