What is the difference between try /except ad if/else?
try runs code which can catch specific exceptions (errors) with catch
And that exception which might create error can be kept in another elif statement…isnt it?
If you write code that might cause an exception (a type of error), one way to handle these exceptions is to wrap them in a
try block. Then if there is an exception is caused in that block, you can take steps to recover from this error by “catching” the error with the
If you don’t catch an exception, it will stop your execution.
else is a way to execute certain parts of code depending on a condition.
You can catch multiple different types of Exceptions by using multiple
except blocks after a single
try: # something except TypeOfException: # do something except AnotherTypeOfException: # do something else
try and except use to handle the errors in the code while if/else used to execute the code. If u get error in if else block then it will be there but using try and catch you can pass the error