Difference between try/except and if/else?

What is the difference between try /except ad if/else?

1 Like

try runs code which can catch specific exceptions (errors) with catch

1 Like

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 except block.
If you don’t catch an exception, it will stop your execution.

In contrast, if/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 block.


    # 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