Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Functions and Looping Expecting Exceptions

Soham Haryani
Soham Haryani
262 Points

Try and except

Why don't we use if-else statements instead of try and except

2 Answers

Hi, I'm still learning Python as well, so my answer might be super basic.

Personally I think it depends on the situation. The if/else statements works better if a specified condition is true or false. So it relies on you predicting when/what error will happen. Errors you expect.

The try/except works better when you test your code and python tells you the error you encounter. The error is handled in the except block, this way your code won't stop because of an error.

So far this is all I can give you. I'm sure there might be an in-depth answer out there.

Hey Soham. I'm not too experienced with python, but I think have answer to your question. We use if-else because it like if something is not the answer to this_. Then there would be a need for a second command if the answer to that is false. Lets say 2+2=4. We would do "if 2+2 +==5" then do so and so. 2+2 is 4 not 5. So the answer would be false. There is a need for a second command in this case. So there would be a Else command.