Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
raising multiple exceptions
#6
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:

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
Reply


Messages In This Thread
raising multiple exceptions - by Skaperen - Mar-18-2017, 03:46 AM
RE: raising multiple exceptions - by wavic - Mar-18-2017, 12:31 PM
RE: raising multiple exceptions - by metulburr - Mar-18-2017, 12:45 PM
RE: raising multiple exceptions - by wavic - Mar-18-2017, 01:41 PM
RE: raising multiple exceptions - by zivoni - Mar-18-2017, 01:53 PM
RE: raising multiple exceptions - by ichabod801 - Mar-18-2017, 02:00 PM
RE: raising multiple exceptions - by wavic - Mar-18-2017, 02:52 PM
RE: raising multiple exceptions - by Skaperen - Mar-19-2017, 02:45 AM
RE: raising multiple exceptions - by wavic - Mar-19-2017, 04:19 AM
RE: raising multiple exceptions - by Skaperen - Mar-19-2017, 05:21 AM
RE: raising multiple exceptions - by wavic - Mar-19-2017, 06:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PiCamera - print exceptions? korenron 2 863 Dec-15-2022, 10:48 PM
Last Post: Larz60+
  SystemError: initialization of integer failed without raising an exception Anldra12 2 4,462 Apr-19-2022, 10:50 AM
Last Post: Anldra12
  Class exceptions DPaul 1 1,324 Mar-11-2022, 09:01 AM
Last Post: Gribouillis
  is this a good way to catch exceptions? korenron 14 4,785 Jul-05-2021, 06:20 PM
Last Post: hussaind
  Raising numbers to power ** GJG 3 2,476 Mar-23-2021, 03:43 PM
Last Post: deanhystad
  Python, exceptions KingKhan248 6 3,097 Nov-15-2020, 06:54 AM
Last Post: buran
  Split string between two different delimiters, with exceptions DreamingInsanity 2 2,058 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  handling 2 exceptions at once Skaperen 2 2,340 Jun-27-2020, 08:55 AM
Last Post: Yoriz
  remove spaces with exceptions catosp 4 2,457 May-29-2020, 09:32 AM
Last Post: catosp
  Looking for advice and Guidance on Exceptions used within Functions paul41 1 2,183 Nov-14-2019, 12:33 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020