A. B A. B - 3 years ago 125
Python Question

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?

Answer Source

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>>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download