Python Forum
Looking for advice and Guidance on Exceptions used within Functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looking for advice and Guidance on Exceptions used within Functions
I understand the basics on Exception handling. However, I am simply looking for the correct way of using the Exception handling when it comes to also using functions.

Say I have created a function named openFile will open a file:

def openFile
    with open('myfile.txt') as fh:
        file_data =
except FileNotFoundError:
    print('The data file is missing')
except PermissionError:
    print('There is a permission error')
except Exception as err:
    print('Some other error has occurred:', str(err))
Within my main I call this function but my main does something with the data for example:

def main():
file = 'openme.txt'
contents = openFile(filename)

Now say the filename does not exist and I hit the FilenotFoundError which is correctly handled and reported back. However, the code within the main continues and then attempts to print the contents.

Question 1: Should I have exception handling within a function like this? How can I get it to stop executing the rest of the code when it returns back to the main? If I add exit(1) to the exception like I have done above for the catch all other exception types this seems to work, but just wondering if this is the correct thing to do?

An alternative is I could add all of the exception handling within the main instead of having it within the function itself. However, this would make the main script very large when its fully completed.

I am really trying to understand where to use exception handling within functions or should they be used within your main part of the scripts.

Be grateful for any advice
after last exception, add a raise statement to catch any other exceptions and terminate the program

I usually keep the the number of exceptions to one, that being the most likely to happen,
again, after the except, add a raise statement to catch any other exceptions

Possibly Related Threads…
Thread Author Replies Views Last Post
  Your Guidance caslor 1 281 Mar-28-2021, 09:34 PM
Last Post: Larz60+
  Python, exceptions KingKhan248 6 466 Nov-15-2020, 06:54 AM
Last Post: buran
  Split string between two different delimiters, with exceptions DreamingInsanity 2 515 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  handling 2 exceptions at once Skaperen 2 587 Jun-27-2020, 08:55 AM
Last Post: Yoriz
  remove spaces with exceptions catosp 4 644 May-29-2020, 09:32 AM
Last Post: catosp
  Noob needing guidance.... bako 0 524 Mar-29-2020, 06:55 PM
Last Post: bako
  New Python learner looking for key word guidance naga 2 554 Nov-20-2019, 01:09 PM
Last Post: ChislaineWijdeven
  multi-line messages in raised exceptions? Skaperen 3 1,930 Aug-01-2019, 02:17 AM
Last Post: Skaperen
  Change linenumber and filename printed in exceptions like #line in C kryptomatrix 2 840 Jul-12-2019, 06:01 AM
Last Post: Gribouillis
  Guidance for how to monitor for a change in a bit state from a hex value FP4A 3 996 Apr-07-2019, 12:13 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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