简体   繁体   中英

Where should I print my output at this simple exception handling?(Python)

Hello guys is there any differences between print my output in "try" clause or putting it after "except" clause with "else:"? here is the code:

try:
    Value1 = int(input("Type the first number: "))
    Value2 = int(input("Type the second number: "))
    Output = Value1 / Value2
except ZeroDivisionError:
    print("Attempted to divide by zero!")
else:
    print(Output)

or this?

try:
    Value1 = int(input("Type the first number: "))
    Value2 = int(input("Type the second number: "))
    Output = Value1 / Value2
    print(Output)
except ZeroDivisionError:
    print("Attempted to divide by zero!")

I mean which one is better? because the result is same. Thanks.

The else clause is run only when no exception is thrown.

So the reason why you'd want to put it in there is to make it explicit: you only want to print the output if there was no exception .

As you mentioned, in your code, there's no functional difference to what happens.

See the docs for more information.

The first one will work fine as per your expectations (assuming that you don't want to bring up the python error prompt and halt the program). It simply prescribes that IF 2nd digit is zero then it won't print the Python error prompt and pass it to the print command (And that's how it should be). Otherwise, in every other case, no matter whatever the divisor is, it will always give an output, so that way you eliminate nearly all loopholes.

Suggestion: Keep the input type as float instead of int, that way you'll be able to print the division for decimal numbers input also. Ex-2/3

Like you already know we are talking about error handling when we are using try...except .
When an error is generated by an operation (or other statements) Python will stop the try block execution and is passed down to the first except block that matches the raised exception. In case there isn't an except clause that matches our exception, it is passed on the outer try statement. This until it's handled or no handler is found, the raised exception becomes an unhandled exception and execution stops with a message of the error traceback.

In addition to except block we can use a finally block , that will be executed regardless of whether an exception occurs, and else block. The last one is useful for code that must be executed if the try clause does not raise an exception.

Your examples

How you said this two pieces of code gives the same result. However, with we read on documentation page of Python we have this affirmation:

" The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn't raised by the code being protected by the try … except statement. "

Simple speaking if you have different statements that raises the same error, but for one them you aren't interested in catching it, move it to else clause. Look on this question on stack to understand better.

So in your case you can let print statement in try block because you will not catch some particular exceptions from it and there isn't much difference in this case where you put the print statement. However, I think the second example is a good logic separation of type "If no errors where founded then let's execute print".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM