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 trialTemi Folorunsho
2,849 PointsExpecting and Raising exceptions
From the practice exercise i saw that i can have more than one exceptions in the try block but, instead of using the raise keyword we keep on using only except. Why?
Temi Folorunsho
2,849 PointsI guess you are right. Thanks
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsIn general, raise
is used to signal that your code found an error that needs handling and try / except
is used to handle an error that may arise from uncontrollable input from outside your code.
Sometimes you raise and handle an error within your own code. You might be tempted to simply handle the error without the try/raise/except structure. Using the try/raise/except structure helps define the code section as an error handling situation and adds readability.
Now to your question: It may be possible that more than one error may occur from calling exterior code. Stacking except
statements allows you to handle each type separately. Stacking except
statements to cover a block of your own code is sometimes used so you donβt have to wrap each line of the block in its own try/except statement.
Post back if you need more help. Good luck!!!
Chaquayla Halmon
1,797 PointsChaquayla Halmon
1,797 PointsI'm still learning Python so this might be a basic explanation. You use EXCEPT when you encounter an error with your code, usually the error would come from python itself. You use RAISE for signaling an exception that YOU want to point out. So in other words YOU are causing this exception/error. In other words your code will still run without the RAISE exception but if you remove the TRY-EXCEPT statement and exception would be raised because of some error.
That is my understanding. Hopefully someone can elaborate a bit more and we can both continue to learn more. EXCELLENT QUESTION!