关于异常处理 [英] Regarding exception handling

查看:54
本文介绍了关于异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


在一个程序中,我想确保一个套接字关闭(所以我最后使用try ...

),但我也想捕获/处理套接字异常。这是我所做的



试试:

试试:

s = .. 。#socket打开


#各种代码......

除了socket.error,x:

#exception handling

最后:

s.close()#socket关闭


是否有更多优雅的或适当的或这样做的方法?或者上面的代码

还可以吗?


提前致谢。

Hello.

In a program, I want to ensure that a socket closes (so I use try ...
finally), but I also want to catch/handle a socket exception. This is
what I have done:

try:
try:
s = ... # socket opens

# various code ...
except socket.error, x:
# exception handling
finally:
s.close() # socket closes

Is there a more "elegant" or "proper" way to do this? Or the above code
is OK?

Thanks in advance.

推荐答案

(我不知道为什么,但是一旦发布,缩进就没有保留。)

(I don''t know why, but indentation was not preserved once I posted.)


Aggelos I. Orfanakos写道:
Aggelos I. Orfanakos wrote:
(我不知道为什么,但是一旦发布,缩进就没有保留。)
(I don''t know why, but indentation was not preserved once I posted.)




这个问题及其可能的解决方案是这里讨论了

线程OT:Google Groups中的代码间距。



This problem and its possible solutions was discussed here in the
thread "OT: spacing of code in Google Groups".


谢谢。现在应该没问题了:


#try:

#try:

#s = ... #socket打开



##各种代码......

#除了socket.error,x:

## exception处理

#finally:

#s.close()#socket关闭

Thanks. This should now be OK:

#try:
# try:
# s = ... # socket opens
#
# # various code ...
# except socket.error, x:
# # exception handling
#finally:
# s.close() # socket closes


这篇关于关于异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆