Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
try/except blocks
#7
(Oct-05-2019, 07:57 AM)buran Wrote: What you actually try to do in this case is to customize the error message.

Yeah, thank you! You're completely right! You express yourself better than I do!!!

(Oct-05-2019, 07:57 AM)buran Wrote: You should re-raise the exception with your custom message
Please, bear with me. Some concepts are easier for me to handle, while others (even being simpler) make me think of myself as rather thick ... Cry
I'm not quite sure what exactly these technical terms mean.

Ok, so:

Errors detected during execution are called exceptions.

And in this context, the technical expression 'to raise an exception' means 'to mention something for somebody to discuss or to deal with' and not as I wrongly thought, 'to end a restriction on somebody or something (like to raise a blockade, a ban, an embargo)', which would have meant here, to let the 'error detected during execution' go with no problems.

So, if I have understood correctly,

(Oct-05-2019, 07:57 AM)buran Wrote: You should re-raise the exception with your custom message

means to bring into attention again the error detected during execution, with my tailor-made message.

(Sorry, I'm always amazed at how our language means something completely different when applied to computing. Take 'argument' as an example ... Tongue ).
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.
Fantastic!!! That is EXACTLY what I wanted, and you made it possible!!! Thanks again!

As I am very thick and I wanted to understand what every little piece of code does, I ran separately these two bits of code (as programs, not as a line command):

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.')
and:

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)
Both producing an:

Error:
SyntaxError: unexpected EOF while parsing.
I looked for it, and it seems that it is usually due to:
1) Missing something like a closing parenthesis (I checked, I copied and pasted it, and I also typed it again, and it doesn't seem to be the case).
2) The SyntaxError: unexpected EOF while parsing means that the end of your source code was reached before all code blocks were completed like a code block starting with a statement as 'for i in range(100):' and requiring at least one line afterwards that contains code that should be in it.

I'm more inclined to think that it is due to something related to the second reason, but still I don't see the reason for that Syntax Error. Am I doing something wrong again or am I missing something important in here?

Anyway, thanks a lot indeed for your great explanation and for solving my dilemma! I still don't fully understand it, but I guess with more learning and coding on my part, I'll do it sooner or later.

All the best,

(Oct-05-2019, 08:39 AM)Gribouillis Wrote: What are the options for the client code?
[ ... ]
It is the strategy used in the C implementation of python with the PyErr_SetString() and PyErr_Occurred() functions but that's another story.
I'm still a newbie, and have no previous programming background, so while I can do a few things here and there, I get lost at most of the technical jargon. Cry

I really do appreciate your time and effort trying to explain it to me. Thank you!

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
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 613 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,154 Dec-12-2022, 08:22 PM
Last Post: jh67
  Am I a retard - else and finally blocks in a try statement RubenF85 6 2,812 Jan-12-2021, 05:56 PM
Last Post: bowlofred
  How to tabulate correctly repeated blocks? Xiesxes 4 3,119 Mar-21-2020, 04:57 PM
Last Post: Xiesxes
  Understanding program blocks newbieAuggie2019 2 2,119 Oct-02-2019, 06:22 PM
Last Post: newbieAuggie2019
  The Empty List Blocks Recursion leoahum 6 5,567 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