Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试除通过外:在右下角书写不会像我期望的那样失败_Python_Try Catch_Python Curses - Fatal编程技术网

Python 尝试除通过外:在右下角书写不会像我期望的那样失败

Python 尝试除通过外:在右下角书写不会像我期望的那样失败,python,try-catch,python-curses,Python,Try Catch,Python Curses,在python中使用curses模块时,我知道在右下角写入会引起错误(这与光标没有“下一个位置”有关) 但我不在乎;我只想在可能的时候写一个角色,否则没什么大不了的。所以我想到了使用try-except-pass构造 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import curses def doStuff(stdscr): Y , X = stdscr.getmaxyx() # get screen size

在python中使用curses模块时,我知道在右下角写入会引起错误(这与光标没有“下一个位置”有关)

但我不在乎;我只想在可能的时候写一个角色,否则没什么大不了的。所以我想到了使用try-except-pass构造

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import curses

def doStuff(stdscr):
    Y , X = stdscr.getmaxyx()       # get screen size

    try:
        stdscr.addch(Y-1,X-1,'X')   # try to print in forbiden corner
    except:
        pass                        # do nothing when error is raised

    stdscr.refresh()
    stdscr.getch()

curses.wrapper(doStuff)
我认为它只会“通过”而忽略“addch”指令。 但令我惊讶的是,这个角色是印刷出来的

有人能给我解释一下原因吗

ps:我确认,没有try-except构造,我得到:

_curses.error:addch()返回错误


Try-catch不忽略方法。它在正常流中执行它,如果引发异常,它将处理它。考虑如下:

def raising_method():
    print "a"
    print "b"
    raise Exception

try:
    raising_method()
except Exception as e:
    pass
“a”和“b”仍将打印


这可能是你的问题吗?我不知道诅咒模块,所以无法告诉您模块的具体信息。

我明白了。。。因此,这并不是真的与诅咒有关。我认为try-except的行为类似于某种if-else循环,因此,如果出现错误,将只执行“except”块中的指令。谢谢你的回答。为了使这个curses更具体,这里的混淆是因为curses只有在右下角打印完字符后才会引发这个异常。try块在引发异常时立即终止,然后except pass块运行(不执行任何操作)。但是curses并不能撤销这样一个事实,即它在终端上打印了一些东西,因为后来捕获到了异常。