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
#1
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
try:
    with open('myfile.txt') as fh:
        file_data = fh.read()
    print(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))
    exit(1)
Within my main I call this function but my main does something with the data for example:

[python]
def main():
file = 'openme.txt'
contents = openFile(filename)
print(contents)
[python]

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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PiCamera - print exceptions? korenron 2 790 Dec-15-2022, 10:48 PM
Last Post: Larz60+
  Class exceptions DPaul 1 1,257 Mar-11-2022, 09:01 AM
Last Post: Gribouillis
  Need some coding guidance for a task peny 5 2,129 Sep-27-2021, 02:02 PM
Last Post: peny
  is this a good way to catch exceptions? korenron 14 4,593 Jul-05-2021, 06:20 PM
Last Post: hussaind
  Your Guidance caslor 1 2,100 Mar-28-2021, 09:34 PM
Last Post: Larz60+
  Python, exceptions KingKhan248 6 2,941 Nov-15-2020, 06:54 AM
Last Post: buran
  Split string between two different delimiters, with exceptions DreamingInsanity 2 1,977 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  handling 2 exceptions at once Skaperen 2 2,261 Jun-27-2020, 08:55 AM
Last Post: Yoriz
  remove spaces with exceptions catosp 4 2,357 May-29-2020, 09:32 AM
Last Post: catosp
  Noob needing guidance.... bako 0 1,835 Mar-29-2020, 06:55 PM
Last Post: bako

Forum Jump:

User Panel Messages

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