Mar-18-2017, 02:00 PM
Except I think he wants all the exceptions raised at once. That is, if it's four long and starts with an int, it should raise two exceptions. But you can't really do that, AFAIK. The closest you could get is raising an exception while handling another exception, but that would imply a try/except block somewhere to handle the first exception. I think that would just be a mess:
So if you want all the errors at once, you would want to collect the error conditions without raising exceptions, and then raise one exception if you found any error conditions. However, if you're doing this for development purposes, I would do it with unit tests. Have one test for each condition, and the run all the tests to see which ones throw exceptions.
try: if not 2 <= len(x) <= 3: raise ValueError('Size must be two or three.') except ValueError: if not isinstance(x[0], str): raise ValueError('First item must be string.') if not isinstance(x[0], str): raise ValueError('First item must be string.')You have to check the first item twice, once if there is a length error and once if there isn't. If you have three items to check it becomes huge.
So if you want all the errors at once, you would want to collect the error conditions without raising exceptions, and then raise one exception if you found any error conditions. However, if you're doing this for development purposes, I would do it with unit tests. Have one test for each condition, and the run all the tests to see which ones throw exceptions.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures