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


Messages In This Thread
Looking for advice and Guidance on Exceptions used within Functions - by paul41 - Nov-13-2019, 11:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PiCamera - print exceptions? korenron 2 1,616 Dec-15-2022, 10:48 PM
Last Post: Larz60+
  Class exceptions DPaul 1 1,962 Mar-11-2022, 09:01 AM
Last Post: Gribouillis
  is this a good way to catch exceptions? korenron 14 7,398 Jul-05-2021, 06:20 PM
Last Post: hussaind
  Your Guidance caslor 1 2,907 Mar-28-2021, 09:34 PM
Last Post: Larz60+
  Python, exceptions KingKhan248 6 4,390 Nov-15-2020, 06:54 AM
Last Post: buran
  Split string between two different delimiters, with exceptions DreamingInsanity 2 3,091 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  handling 2 exceptions at once Skaperen 2 3,327 Jun-27-2020, 08:55 AM
Last Post: Yoriz
  remove spaces with exceptions catosp 4 3,367 May-29-2020, 09:32 AM
Last Post: catosp
  multi-line messages in raised exceptions? Skaperen 3 9,571 Aug-01-2019, 02:17 AM
Last Post: Skaperen
  Creating custom exceptions that co-operate LadySvetlana 4 4,201 Mar-19-2019, 04:24 PM
Last Post: LadySvetlana

Forum Jump:

User Panel Messages

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