Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
try/except blocks
#6
The code that uses a function must take into account the specifications of that function. When you design the function, you need to say what the function does when it receives invalid parameters. What are the possibilities?
  1. raise an exception (the type of the exception can be fine-tuned)
  2. return a value that indicates an error (can be None)
  3. return a dummy value
  4. return an unspecified value
What are the options for the client code?
  1. Ignore the error which includes letting an exception or a faulty value propagate
  2. Detect the error and take appropriate action
I think the good strategy is often 1. for the function's design and 1. for the exploiting code. Strategy 1 is usually possible for the client code only if the function's design chose strategy 1. Using strategy 2 in the client code means that this code needs to test return values or catch exceptions as Buran said.

Note that there could be more exotic design option, for example the function could set a global indicator to say that it failed, but this is not relevant in a language that implements exceptions. It is the strategy used in the C implementation of python with the PyErr_SetString() and PyErr_Occurred() functions but that's another story.
Reply


Messages In This Thread
try/except blocks - by newbieAuggie2019 - Oct-05-2019, 02:04 AM
RE: try/except blocks - by wavic - Oct-05-2019, 02:36 AM
RE: try/except blocks - by newbieAuggie2019 - Oct-05-2019, 04:59 AM
RE: try/except blocks - by Larz60+ - Oct-05-2019, 02:57 AM
RE: try/except blocks - by buran - Oct-05-2019, 07:57 AM
RE: try/except blocks - by newbieAuggie2019 - Oct-05-2019, 03:55 PM
RE: try/except blocks - by Gribouillis - Oct-05-2019, 08:39 AM
RE: try/except blocks - by perfringo - Oct-05-2019, 04:42 PM
RE: try/except blocks - by newbieAuggie2019 - Oct-05-2019, 05:55 PM
RE: try/except blocks - by buran - Oct-05-2019, 05:01 PM
RE: try/except blocks - by newbieAuggie2019 - Oct-05-2019, 05:21 PM
RE: try/except blocks - by buran - Oct-05-2019, 05:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  RSA Cipher with blocks Paragoon2 0 545 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,986 Dec-12-2022, 08:22 PM
Last Post: jh67
  Am I a retard - else and finally blocks in a try statement RubenF85 6 2,672 Jan-12-2021, 05:56 PM
Last Post: bowlofred
  How to tabulate correctly repeated blocks? Xiesxes 4 3,008 Mar-21-2020, 04:57 PM
Last Post: Xiesxes
  Understanding program blocks newbieAuggie2019 2 2,046 Oct-02-2019, 06:22 PM
Last Post: newbieAuggie2019
  The Empty List Blocks Recursion leoahum 6 5,432 Mar-05-2019, 06:55 PM
Last Post: leoahum

Forum Jump:

User Panel Messages

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