简体   繁体   English

通过这种简单的异常处理,我应该在哪里打印输出?(Python)

[英]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:"? 大家好,在“ try”子句中打印我的输出还是在“ except”子句后使用“ 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. else子句仅在不引发任何异常时运行。

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). 第一个可以按照您的期望正常工作(假设您不想显示python错误提示并暂停程序)。 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). 它只是简单地规定IF第二位数为零,那么它将不打印Python错误提示并将其传递给print命令(这就是应该的方式)。 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. 建议:保持输入类型为float而不是int,这样您就可以打印除以小数的输入。 Ex-2/3 前三分之二

Like you already know we are talking about error handling when we are using try...except . 就像您已经知道的那样,当我们使用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. 当某个操作(或其他语句)生成错误时,Python将停止try块执行,并向下传递到与引发的异常匹配的第一个除外块 In case there isn't an except clause that matches our exception, it is passed on the outer try statement. 如果没有匹配我们异常的except子句,则在外部try语句中传递它。 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. 除了except块之外,我们还可以使用finally块 ,无论是否发生异常,都将执行finally块 ,else块。 The last one is useful for code that must be executed if the try clause does not raise an exception. 对于try子句未引发异常的必须执行的代码,最后一个有用。

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: 但是,在Python的文档页面上阅读后,我们得到了以下确认:

" 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. " 使用else子句比向try子句添加其他代码更好,因为它避免了意外捕获try ... except语句所保护的代码未引发的异常。

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. 简单来说,如果您有引发同一错误的不同语句,但是对于其中一个语句,您不希望捕获它,请将其移至else子句。 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. 因此,在您的情况下,您可以将print语句放到try块中,因为您不会从中捕获某些特定的异常,并且在这种情况下,您放置print语句并没有太大区别。 However, I think the second example is a good logic separation of type "If no errors where founded then let's execute print". 但是,我认为第二个示例是一个很好的逻辑分离,类型为“如果发现错误,则执行打印”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中放置异常处理的位置 - Where to put exception handling in python 我应该在哪里发布我的python代码? - Where should I post my python code? 对于没有从服务器获取数据或连接超时,我应该如何在我的数据库 python 代码中进行异常处理 - How should I do exception handling in my database python code for no data fetched from server or connection time out 我应该在哪里使用什么例外? URLLib Python3 - Where and What exception I should use ? URLLib Python3 当我不处理异常时如何打印异常? - How to print an exception when I'm not handling it? 我应该如何为我的python应用程序构建一个简单的数据库包? - How should I build a simple database package for my python application? 如何使用异常处理在python中打印指定索引中的数字是正数还是负数? - How can I print whether the number in the specified index is positive or negative in python using exception handling? 我的Python 3模块应该在哪里? - Where should my Python 3 modules be? 我应该在我的python项目中的哪里放置config.txt - Where should I place a config.txt in my python project 我应该把我的python脚本放在Linux中? - Where I should put my python scripts in Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM