Python Forum
raise exception instead of return None
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
raise exception instead of return None
#1
i have a function that in a certain tested case returns None because calculations just didn't work out. instead, i'd rather have it raise an exception. what exception should it raise?
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
I would raise a ValueError
Reply
#3
(Jul-18-2018, 08:51 PM)Skaperen Wrote: just didn't work out
Could you elaborate?

A ValueError would make sense if the function argument(s) were of "an inappropriate type" (according to the link). If that's what you mean by "just didn't work out" then it might apply, but it depends on why they didn't work out.
Reply
#4
they "didn't work out" due to environmental conditions such as insuffient file space or not enough calculation information ... things out of control of the user that the calling script could have checked on.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Insufficient file space would be an IOError. Not enough calculation information sounds like a ValueError. If you know why the error is occurring, it's better to give that information with a more specific error type. If you don't know, you could give the catch all RuntimeError, or you could make your own exception:

class DidntWorkOutError(RuntimeError):
    pass
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SystemError: error return without exception set!!! faryad13 3 3,582 Oct-23-2020, 02:32 PM
Last Post: ATARI_LIVE
  raise exception within generator bermudj 3 2,897 Jun-06-2020, 11:56 AM
Last Post: buran
  can i raise an exception in a try clause? Skaperen 14 5,600 Dec-19-2019, 12:29 AM
Last Post: Skaperen
  Changing a traceback message without a 2nd raise Clunk_Head 1 1,947 Jul-14-2019, 12:45 AM
Last Post: Gribouillis
  During handling of the above exception, another exception occurred Skaperen 7 26,723 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  "Raise SMTPException("SMTP AUTH extension not supported by server.") NajeebUllah 3 7,919 Mar-16-2018, 09:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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