2012-05-29 62 views
0

我想弄清楚如果我不知道如何排除異常,我將如何打印異常。我將如何做以下幾點?在Python中打印空白異常

try: 
    some_command 
except: 
    print *full_exception_trace* 

回答

3
def exception(self) 
    try: 
     Something.objects.all() 
    except Exception, err: 
     print err.message #(if you want) 
     #raise err 
     raise # The 'raise' statement with no arguments inside an error 
       # handler tells Python to re-raise the exception with the 
       # original traceback intact 

err.message會給你異常

+5

我同意答案,但挑剔:在大多數情況下,我會建議你不要'提高錯誤',而只是'提高'。前者會導致回溯指向'raise err'行;後者將導致回溯指向異常最初引發的地方。一個小的細節,但它可以在調試的方便性上有很大的不同。 –

1

的原因的traceback模塊的print_exc()功能似乎是你想要的。 Docs