Python Forum
what type of exception to use?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what type of exception to use?
#1
i have a function that uses some keyword arguments. two of the keyword arguments cannot be used in the same call and if so the function will raise an exception. all of its keyword arguments do take the value None as being the same as that keyword argument not being used at all.

my question is: which exception should be raised in this case?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Depending on the project, I might prefer a custom exception. They're cheap and not going to collide with anything.

But if I needed to use a built in, it seems like you're complaining about the values, so I'd lean toward it being a ValueException.
Reply
#3
I'd choose a TypeError or your own subclass of TypeError, because if you change the values of the arguments without changing their type, there is still an error.
Reply
#4
any and every case of using foo=notNone and also using bar=notNone needs to raise this exception. if only one of them is used then this conflict is avoided but it could instead have a genuine TypeError or ValueError if the one value passed is a bad type or bad value. it is not essential to have 3 different kinds of exceptions. the function will be explicitly testing for these.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Exception: Returned Type Mismatch Error devansing 1 5,144 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  Type hinting - return type based on parameter micseydel 2 2,470 Jan-14-2020, 01:20 AM
Last Post: micseydel
  How to fix 'uncaught exception of type NSException' in Python MonsterPython 0 2,149 Jul-09-2019, 06:52 AM
Last Post: MonsterPython
  Stack trace shows different exception type than print micseydel 5 4,408 Apr-01-2019, 10:24 PM
Last Post: micseydel
  During handling of the above exception, another exception occurred Skaperen 7 26,868 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  choice of exception type Skaperen 1 2,351 Sep-01-2018, 05:50 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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