Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last exception in pdb

Tags:

Is there a way to examine the last exception when in pdb/before entering pdb? (Using python 2.7.5).

Immediately (yes, I enter no other commands at all) after an exception being raised in my code, I do sys.exc_info(); this just results in (None, None, None). At this point, I can do pdb.pm(), and pdb starts at the point that the exception is raised.

I'd like to be able to examine this exception object (it is not stored in a variable before being raised).

There is nothing obviously helpful in http://docs.python.org/2/library/pdb.html or http://docs.python.org/2/library/sys.html

Edit: I know about set_trace. I'd like to examine the exception before I modify the code.

like image 924
Marcin Avatar asked Oct 06 '13 16:10

Marcin


People also ask

What is pdb Set_trace ()?

pdb. set_trace(*, header=None) Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). If given, header is printed to the console just before debugging begins.

What does import pdb pdb Set_trace () do?

Importing the pdb module and running the pdb. set_trace() function lets you begin your program as usual and run the debugger through its execution.

How can you conditionally raise and handle an exception during debugging in Python?

The try-except-finally block is used in Python programs to perform the exception-handling task. Much like that of Java, code that may or may not raise an exception should be placed in the try block.

How do I debug Python in terminal?

Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().


2 Answers

Is this what you are looking for?

import pdb try:     1/0 except Exception as err:     pdb.set_trace() 

% test.py --Return-- > /home/unutbu/pybin/test.py(8)<module>()->None -> pdb.set_trace() (Pdb) err ZeroDivisionError('integer division or modulo by zero',) (Pdb) quit 

If you do not want to modify the code where the exception originates, you could instead redefine sys.excepthook:

import pdb import sys def excepthook(type, value, traceback):     pdb.set_trace() sys.excepthook = excepthook  1/0 

% test.py --Return-- > /home/unutbu/pybin/test.py(7)excepthook()->None -> pdb.set_trace() (Pdb) type <type 'exceptions.ZeroDivisionError'> (Pdb) value ZeroDivisionError('integer division or modulo by zero',) (Pdb) traceback <traceback object at 0xb774f52c> (Pdb)  
like image 106
unutbu Avatar answered Sep 21 '22 13:09

unutbu


You can retrieve the latest exception in pdb/ipdb via:

__exception__ 

The above is actually a tuple of the (exception, message).

like image 22
cdosborn Avatar answered Sep 20 '22 13:09

cdosborn