标签: try-except

c ++ try-except语句

我看到有关检测VMWare或Virtual PC的文章
http://www.codeproject.com/KB/system/VmDetect.aspx
,我看到他们使用了某种try-except语句.
所以我在MSDN中查找了它:http://msdn.microsoft.com/en-us/library/s58ftw19%28v=vs.80%29.aspx

而我不明白为什么我会使用try-except而不是旧的try-catch.它只是给我关于例外的其他信息吗?
如果是这样,当我使用附件中的代码时,我可以使用try-catch,对吗?
谢谢 :)

c++ windows try-catch try-except

4
推荐指数
1
解决办法
2390
查看次数

尝试除了不必要的步骤

使用hello2而不是hello1总是安全的吗?

def hello1():
    try:
        aaa = foo()
        return aaa
    except baz:
        return None   

def hello2():
    try:
        return foo()
    except baz:
        return None   

python performance try-except

4
推荐指数
1
解决办法
82
查看次数

尝试 - 除了未知的功能?

我正在尝试创建一个函数来测试函数是否存在,然后根据它是否存在返回一个布尔值.

这是我的代码; 但是,Python IDLE 3.5告诉我,我的eval()语句有错误,但我没有看到错误:

def testFunction(entity):
    try eval(entity)():
        return True
    except NameError:
        return False
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

python function python-3.x try-except

4
推荐指数
1
解决办法
333
查看次数

try-except block:如果引发异常,则为'else'的模拟

我有这样的代码:

try:
    return make_success_result()
except FirstException:
    handle_first_exception()
    return make_error_result()
except SecondException:
    handle_second_exception()
    return make_error_result()
Run Code Online (Sandbox Code Playgroud)

我想知道有什么办法可以实现这个目标:

try:
    # do something
except Error1:
    # do Error1 specific handling
except Error2:
    # do Error2 specific handling
else:
    # do this if there was no exception
????:
    # ALSO do this if there was ANY of the listed exceptions (e.g. some common error handling)
Run Code Online (Sandbox Code Playgroud)

因此代码以下列顺序之一执行:

try > else > finally
try > except > ???? > finally
Run Code Online (Sandbox Code Playgroud)

编辑:我的观点是????块应该在任何except块之后执行,这意味着它是错误处理的补充,而不是替换.

python exception-handling exception try-except

4
推荐指数
1
解决办法
190
查看次数

尝试 except 递归或 while 循环?

我正在做一个 python 课程,他们建议在 while 循环中使用 try 和 except 块,以便不断请求输入,直到满足条件。直觉上我觉得在“ except”块中再次调用该函数会更短,如下所示:

def exceptiontest():
    try:
        print(int(input("number 1: "))+int(input("number 2:")))
    except:
        print("a mistake happened")
        exceptiontest()

exceptiontest()
Run Code Online (Sandbox Code Playgroud)

当我在论坛上询问课程时,我得到的答复是不一样。我现在有点困惑。有谁可以帮我澄清一下吗?提前致谢!

python error-handling recursion while-loop try-except

4
推荐指数
1
解决办法
819
查看次数

在没有pylint错误的情况下捕获所有异常

import traceback

def func():
    try:
        -- do something --
    except:
        traceback.print_exc()
Run Code Online (Sandbox Code Playgroud)

对于此代码,
pylint报告错误:bare-except No exception type(s) specified , W0702, 当 except 子句未指定要捕获的异常类型时发生。

现在,如果我希望在没有 pylint 错误的情况下捕获所有异常。有没有办法。
请帮忙。
谢谢

pylint python-3.x try-except

4
推荐指数
2
解决办法
4806
查看次数

Python3 中“除了 socket.error as (code, msg)”的等价物是什么?

try/except由于以下行,我在 Python2 中有这个块不能在 Python3 中运行except socket.error as (code, msg)

try:
    (conn, (ip,port)) = tcpServer.accept()
except socket.error as (code, msg):
    if code != errno.EINTR:
        raise
    else:
        break
Run Code Online (Sandbox Code Playgroud)

Python3 中的等价物是什么?有没有一种方法适用于两个 Python 版本?

python try-except

4
推荐指数
1
解决办法
1342
查看次数

python try except 作为计算表达式的函数

我尝试创建一个函数来尝试表达式并在出现错误时返回零。

def try_or_zero(exp):
    try:
        exp
        return exp
    except:
        return 0
Run Code Online (Sandbox Code Playgroud)

这显然是行不通的。问题似乎在于 python 没有任何形式的惰性求值,因此表达式在传递给函数之前会被求值,因此它会在进入函数之前引发错误,因此它永远不会通过 try 逻辑。有谁知道这是否可以在Python中完成?干杯

python python-3.x try-except

4
推荐指数
1
解决办法
1826
查看次数

最后在Python 3生成器中尝试

我遇到了一段Python 3代码:

def gen():
    try:
        while True:
            yield 1
    finally:
        print("stop")

print(next(gen()))
Run Code Online (Sandbox Code Playgroud)

运行它之后,我首先想到的输出应该是:

def gen():
    try:
        while True:
            yield 1
    finally:
        print("stop")

print(next(gen()))
Run Code Online (Sandbox Code Playgroud)

但实际上结果是:

1
Run Code Online (Sandbox Code Playgroud)

怎么会这样 引擎盖下发生了什么?

如果我运行for i in gen(): print(i),将会出现一个无限循环,这正是我所期望的。fornext这里和有什么不一样?

python generator python-3.x try-except

4
推荐指数
2
解决办法
81
查看次数

在 Python 3 中捕获特定的 OSError 异常

在Python 3中,我们如何捕获特定的OSError异常?

我当前的代码捕获了所有OSError,但只OSError: [Errno 12]需要捕获。

try:
    foo()
except OSError as e:
    print('Caught OSError: [Errno12]')
Run Code Online (Sandbox Code Playgroud)

完整的错误消息是:

捕获 OSError: [Errno12] 无法分配内存

我们怎样才能让Python只捕获 的Errno12变体OSError

python error-handling ubuntu python-3.x try-except

4
推荐指数
1
解决办法
7051
查看次数