[no audio]
Friends, here we are going to see how to create custom exceptions.
First of all, what is a custom exception?
Simple, wantedly if you want to raise an exception, then you
can raise. So that type of exceptions are called custom exceptions.
And you can create this custom exceptions in two ways -
by using 'raise' and 'assert'.
So 'raise' we are going to use to raise an exception
which are existing with your Python.
Right. See let me explain this with an example.
So I am going to save my script name as something like
'custom_exceptions', right. See simply I am using a 'raise'
[no audio]
'Exception("This is a exception"). That's it.
Now see the output. If I run this
it is giving like an exception, right. See what is the exception
name? "Exception" itself.
And what is the error?
"This is a Exception". Not only that, suppose if any variable
is not defined you know, right. "NameError" is the exception.
So, "Variable is not defined". Wantedly
I'm raising this.
[no audio]
That's it.
But this is not the way to use in your real-time. Suppose,
just assume that you are trying to read a variable.
It may be any variable. Let's say simply 'age=' some 23.
So what I want to do is, 'if age > 30:' then
I don't have any problem.
'print("Valid age")'.
But if 'age' is not less than, I mean not greater
than 30 then I want to raise an exception.
Instead of printing simply a message wantedly now, actually
previously we have studied to avoid exceptions,
but now we are trying to learn how to create exceptions if
it is required.
Right. Suppose if age is less than 30 then I don't want to
run my script. I want to terminate my script.
I want to stop. I need to raise an exception.
Yes, you can raise, simply raise.
So I am taking suppose, 'ValueError' exception.
Right. So see I am using existing keywords, 'ValueError' that
is already there with your exceptions, right?
So that's why 'raise' keyword is used to raise an existing exception wantedly.
"Age is less than 30".
That's it.
This is the way how we can raise a custom exception using
'raise' keyword.
Now we have one more thing called 'assert'.
So 'assert' is going to be used to create your own exceptions with
a name called 'AssertError'. First
let me do that.
See I am writing simply 'assert'.
See, first of all how your 'assert' is going to work, 'assert'
will raise assertion error, if the condition becomes false.
I mean suppose I am writing 4 < 3, that is false.
But if I write 4 > 3, that is correct, right. Now
see that I am running,
[no audio]
it is not raising any exception because condition is true,
right. If I write condition as false, now see the result what
you are getting. You are getting error called, exception
called "AssertionError". That's it.
So 'assert' is used to create "AssertionError", and whenever
if you get any "AssertionError" you have to assume that
wantedly you created
an exception, that's it.
[no audio]
Right. So here also you can take something like, suppose
[no audio]
if instead of 'if' condition, now directly you can give it. So
suppose I am trying to write 'try:', actually assume that I am
writing here suppose 'age=30'.
Right. So now I no need to use 'if' condition. Directly if I want
to raise an exception based on 'age' then I no need to use 'if'
condition, 'assert' itself is going to work as 'assert',
if 'age < 30', then only create,
create an exception. If it is true then see,
[no audio]
"Valid age".
Let me run it and see the result.
Yeah, this is 'try' and 'except', right?
You need to write your 'except' block as well.
Let me write 'except:'.
'print("Exception
occurred")', right.
So what type of assertion, sorry what type of exception
you're creating with the help of 'assert', 'AssertionError'.
Let me run it and see the result.
[no audio]
See both are same. Let me take 33, then condition will become true, right?
I mean, now see what you are getting, if age is, if it is less than 30
[no audio]
Yeah, if it is greater than, right, as per your requirement.
If 'age' is greater than 30, no problem.
Right. Let me take my age as something like 20,
[no audio]
now if 'age' is not greater than 30, right, then
I want to create an 'AssertionError'.
Yes, exception is occurring means 'AssertionError' is running,
raising. So if you want to handle your raised 'AssertionError',
yes, you can handle it like, 'except: if error is 'AssertionError'
then 'print("Raised with assert because age is lessthan 30")'.
That's it. Now see the result.
[no audio]
So 'assert' is going to give an exception called
'AssertionError'. That's it.
Right. So guys for time being just have a look about this
'assert' and 'raise', right.
So while writing real-time scripts based on requirement,
I will use 'raise' sometime,
I will use 'assert' sometime, and simply I will use 'try:' and
'except:'. It's based on situation.
So up to that you have to remember the syntaxes, right and
then we will use there while writing our scripts.
Okay guys, thank you for watching this video.
[no audio]