简体   繁体   English

我可以从pdb引发异常吗? (用于调试)

[英]Can I raise an exception from pdb? (for debugging)

Let's say I have: 比方说我有:

def fn1():
    # do some work
    # in some cases raise exception

def fn2():
    # do some work
    try:
        fn1()
    except:
        # do some work

I want to test the negative path by raising an exception from within pdb. 我想通过从pdb中引发异常来测试负路径。 I tried from fn1 : 我试过fn1

(Pdb) raise cliexceptions.NoConnectionError("Could not connect")
*** NoConnectionError: 'Could not connect to SSR'

It prints the msg but does not exit fn1 . 它打印msg但不退出fn1 Is there a way to do that? 有没有办法做到这一点?

You can manually (partially) simulate the impact of an exception, by jumping to the appropriate line in the except clause, and in case you also catch an exception object (which in your case you don't), explicitly create it. 您可以通过跳转到except子句中的相应行来手动(部分)模拟异常的影响,如果您还捕获异常对象(在您的情况下没有),则显式创建它。

# a.py
line1@ def fn2():
line2@    # do some work
line3@    try:
line4@        fn1()
line5@    except:
line6@        print 'exception'

(Pdb) b 4
(Pdb) c
> a.py(4)fn2()
-> fn1()
(Pdb) j 6
> a.py(6)fn2()
-> print 'exception'
(Pdb) e = ValueError(5)
(Pdb) <<the rest of your debugging here>>

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

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