Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
try/except blocks
#5
What you actually try to do in this case is to customize the error message. You should re-raise the exception with your custom message

def division1(divideBy):
    try:
        return 72 / divideBy
    except ZeroDivisionError:
        raise ZeroDivisionError('72 divided by 0 produces an Error: Invalid argument.')

for n in (2, 12, 0, 1):
    try:
        print(f'72 divided by {n} is {division1(n)}.')
    except ZeroDivisionError as zde:
        print(zde)
Output:
72 divided by 2 is 36.0. 72 divided by 12 is 6.0. 72 divided by 0 produces an Error: Invalid argument. 72 divided by 1 is 72.0.
of course what you would really do is
for n in (2, 12, 0, 1):
    try:
        print(f'72 divided by {n} is {72 / n)}.')
    except ZeroDivisionError:
        print ('72 divided by 0 produces an Error: Invalid argument.')
If you like/need you may wrap it as function
def divide72(divisor):
    try:
        print(f'72 divided by {n} is {72 / divisor)}.')
    except ZeroDivisionError:
        print ('72 divided by 0 produces an Error: Invalid argument.')
for n in (2, 12, 0, 1):
    divide72(n)
I understand though that this function is just an example
In other words, you should propagate the error from your function and catch it when you call the function.

Of course there might be cases when you just want to catch the error (e.g. in mid loop) print some message and continue the execution of the function/loop
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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 598 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 2,120 Dec-12-2022, 08:22 PM
Last Post: jh67
  Am I a retard - else and finally blocks in a try statement RubenF85 6 2,763 Jan-12-2021, 05:56 PM
Last Post: bowlofred
  How to tabulate correctly repeated blocks? Xiesxes 4 3,094 Mar-21-2020, 04:57 PM
Last Post: Xiesxes
  Understanding program blocks newbieAuggie2019 2 2,096 Oct-02-2019, 06:22 PM
Last Post: newbieAuggie2019
  The Empty List Blocks Recursion leoahum 6 5,527 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