Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.

Nick Mitias
Nick Mitias
1,845 Points

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.