简体   繁体   English

尝试/除非在编译的Python中不捕获错误

[英]Try/except not catching errors in compiled Python

I've got a bit of code involving Try and Except clauses. 我有一些涉及Try和Except子句的代码。 When I just run it in console in IDLE, it works perfectly, and never crashes. 当我在IDLE的控制台中运行它时,它可以完美运行,并且从不崩溃。 However, when I compile it is fails to catch errors, so the program crashes, and this is driving me mad! 但是,当我编译时,它无法捕获错误,因此程序崩溃了,这使我发疯!

The code is: 代码是:

if self.height == 6:
    try:
        libtcod.path_compute(minimap[self.mapx][self.mapy].path3,self.x+60, self.y+60,target_x+60, target_y+60)
        stepx, stepy = libtcod.path_get(minimap[self.mapx][self.mapy].path3, 0)
        dx = stepx - self.x - 60
        dy = stepy - self.y - 60
    except:
        success = False  

I'm using the libtcod library. 我正在使用libtcod库。 It's the stepx/stepy bit it sometimes fails on, and the thing is, I know it can sometimes fail on that bit; 这是有时可能失败的stepx / stepy位,而事实是,我知道有时可能会失败。 it just doesn't catch it! 它只是没有抓住它! I've tried having it print a message before/after the stepx section, and it'll print the first part, but not the latter. 我试过让它在stepx部分之前/之后打印一条消息,它将打印第一部分,但不打印第二部分。 There's a multitude of options that can lead to success = False, and this only one, and then later I deal with what happens if success == False (it's for a game, testing if a foe can path to you, and if not, then it tries something else). 有很多选项可以导致成功= False,只有一个选项,然后我再处理成功== False时发生的情况(这是针对游戏的,测试敌人是否可以通向您,如果不是,然后尝试其他操作)。 I have no idea why it can catch it when uncompiled but fails totally when compiled. 我不知道为什么它在未编译时可以捕获它,但是在编译时完全失败。 Can someone please explain this to me? 有人可以向我解释一下吗?

Thanks in advance! 提前致谢!

Try checking all the arguments that you pass to the libtcod function that crashes your program (in this case libtcod.path_get ). 尝试检查传递给崩溃程序的libtcod函数的所有参数(在本例中为libtcod.path_get )。 Libtcod will often segfault when you pass in incorrect arguments rather than raising a nice error message, I had the same problem with the field of view toolkit. 当您传递不正确的参数而不是发出漂亮的错误消息时,Libtcod通常会出现段错误,我在视野工具箱中也遇到了同样的问题。

If these values are different in your version "compiled" with py2exe for some reason then that could explain why it only crashes sometimes. 如果这些值由于某种原因在用py2exe“编译”的版本中不同,则可以解释为什么有时它仅崩溃。

This part looks problematic: 这部分看起来有问题:

minimap[self.mapx][self.mapy]

If you have a nested array like so: 如果您有这样的嵌套数组:

minimap = [[1, 2, 3, 4],
           [5, 6, 7, 8],
           [9, 10, 11, 12]]

Then to refer to the item at position (x, y) you must use minimap[y][x] 然后,要引用位置(x,y)的项目,必须使用minimap[y][x]

Edit: 编辑:

libtcod.path_get 's first argument should be a map used for path-finding, which is returned by path_new_using_map or dijkstra_new . libtcod.path_get的第一个参数应该是用于路径查找的映射,该映射由path_new_using_mapdijkstra_new返回。 You don't need a separate map for each square in your map. 您不需要为地图中的每个正方形单独绘制地图。 I think you should read the documentation a little more thoroughly: http://doryen.eptalys.net/data/libtcod/doc/1.5.0/index.html 我认为您应该更彻底地阅读文档: http : //doryen.eptalys.net/data/libtcod/doc/1.5.0/index.html

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

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