Python Forum
exception handling by default
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
exception handling by default
#1
i am making a function which does something that may raise an exception. normally i would have the caller handle the exception. but i have cases where the function needs to handle the exception if the caller is not handling it. an example case is the function performing printing with the possibility of BrokenPipeError being raised. the caller may want to handle it and quickly exit when it happens. but, it is not handled by the caller, i want the function to handle the exception inside the function to avoid causing the script to be aborted (to allow the caller to finish in case it has critical steps it must do at the end). how can i make the function handle an exception and allow the caller handling of it take precedent?

the printing case is just one example.
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
Use a try/except BrokenPipeError in your function and handle it.
Reply
#3
It sounds like what you're trying to do is have a function fdepend on the behaviour of its caller g. That at the very least sounds like a bad thing, because now f is more complex and that means harder to understand, test and change. There are probably better explanations of why to avoid this sort of thing - the kind of phrase to search for is "high level modules depend on low level modules" (and not the other way round - here f is the lower level one). You can probably find a better design, which may just be letting the caller handle the failures that the function can't handle.
Reply
#4
(Sep-11-2021, 10:38 PM)Yoriz Wrote: Use a try/except BrokenPipeError in your function and handle it.

that is what i have been doing. then the caller does not get that exception if it wants to do something particular when it happens. i think KeyboardInterrupt could have similar issues (maybe the caller is designed to save some data at that event but it happens during a function doing try/except around something long enough to catch it).

it seems to me that the notion of inner code taking precedence over outer code is backwards in some cases.
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
(Sep-12-2021, 01:19 PM)ndc85430 Wrote: You can probably find a better design, which may just be letting the caller handle the failures that the function can't handle.

but the function can handle it and needs to in some cases. i do have that thought that caller might well need to have the burden to handle things like BrokenPipeError. but KeyboardInterrupt might be a different matter. read my previous reply to Yoriz.
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
  while handling this exception, another ... Skaperen 1 1,785 Aug-22-2021, 06:16 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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