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 trialHanneke Lu
560 PointsDifficulty understanding "ValueError as err"
I've thoroughly studied the previous questions and answers about this subject. But I still don't fully understand it.
It's hard to explain what it is I need to hear, so if any of you feels they could explain the whole concept then I'd be thrilled.
Otherwise, the answer to the following question may help me on the way:
Why is the message from the raise ValueError ("More than one person is required to split the check") not displayed? (when Craig says: 'now we lost our messaging')
Why is it displayed as soon as Craig changes the 'except ValueError' into 'except ValueError as err'?
I hope you can help! I'm a beginner so if it's possible to use layman's terms, please do so (I'm not familiar with terms like 'aliasing' yet. I did try to figure stuff out on the internet for about an hour, before asking this question. But figured I really need your help!)
4 Answers
Mark Kastelic
8,147 PointsHi Hanneke,
Initially, the split_check function is not called within the try block. As a result, the ValueError is handled exclusively by the function with the raise statement. When Craig moves the split_check function call into the try block, the ValueError is now handled by the except statement in the try block. The message is lost because the try block caught the ValueError raised by the function, and only needs to handle it by executing the single print statement in the except block. In order to "preserve" the string attached to the raise ValueError within the function, it has to be saved as a variable (commonly as "err"). Now, when Craig adds a second print statement to the except block, the contents of err are printed out as well.
Annie Livingstone
1,844 PointsI had the same question as Henneke. Marks answer was very helpful! However, I feel like am still missing something. Maybe someone could help me! I'm not really seeing how the use of the variable err is referencing the statement in the function. I don't see where err was defined. I feel like it may be an obvious answer, but for some reason its not coming to me! Thanks
Jonathan Kobylanski
2,181 PointsHi Annie, This tripped me up too and I gave it a good hard look. I have a feeling that as Mark mentioned above without the "as" which is the point the err is set as a variable the ValueError handling is stoped. Thus the script will not look for anything ells to handle more ValueErrors.
So... to print the "raise" for a ValueError within the function split_check you need to print the next ValueError. This is where you use the "as" to set the next ValueError to the variable err and thus you can print out the text assigned to the ValueError that is held within the function. ( This is my only way of slightly making sense of it all, I hope it helps )
Hanneke Lu
560 PointsHi Mark, Thank you very much for your explanation! Wishing you a nice weekend! Hanneke
Hanneke Lu
560 PointsHanneke Lu
560 PointsIt's at 3:00 in the video, that Craig says 'We lost our messaging now' (and there he lost me, too...)