简体   繁体   中英

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. I tried from fn1 :

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

It prints the msg but does not exit 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.

# 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>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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