EXCEPTION HANDLING
Hello guys, we here bring another blog which will help us in our
competitive programming the most.
The topic is, “exception handling”. Exceptions in python are objects that represent errors; they can be raised by passing invalid arguments to function or performing certain illegal operations or by explicitly.
It actually
returns back the information about the error and how it was raised.
A basic example,
Output
We handle the
exception using 4 statements, they go as:
a)
Try: This statement lets you test a code
for errors.
b)
Except: This statement lets you handle the
errors. This block is required even if the code passes
c)
Else: This statement will work if the code doesn’t
have any error.
d)
Finally: This statement will be executed
regardless of the presence of errors
Take a look at another example,
Output
Exception handling is very important when it comes to files in python.
Check this out,
Raise an Exception
As a
programmer we can choose to throw an exception if conditions occur.
The keyword used is, raise.
Look at the example,
Output
Similarly we
can define what type of error we can throw.
Look at the code below,
Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
Do subscribe to our daily blog update by clicking here.
Thank You!
No comments:
Post a Comment