SORU
29 AĞUSTOS 2009, CUMARTESİ


&; İç özel durum" (traceback) Python"?

Benim arka plan C# ve sadece son zamanlarda Python programlamaya başladım. Bir özel durum oluştuğunda, ben genellikle hala tam yığın izleme gösterirken daha fazla bilgi ekleyen bir istisna sarın. C oldukça kolay# ama nasıl Python ile yapabilir miyim?

Örn. C# ben böyle bir şey yapar:

try
{
  ProcessFile(filePath);
}
catch (Exception ex)
{
  throw new ApplicationException("Failed to process file "   filePath, ex);
}

Python ile benzer bir şey yapabilirim:

try:
  ProcessFile(filePath)
except Exception as e:
  raise Exception('Failed to process file '   filePath, e)

...ama bu iç özel durum traceback kaybeder!

Düzenleme:Özel durum mesajları ve hem de yığın izlemelerini ve iki ilişkilendirmek görmek istiyorum. O çıktı, bu durum, görmek istiyorum X C gibi burada ve daha sonra özel durum Y - aynı oluştu#. Bu Python 2.6 mümkün mü? Şimdilik yapabileceğim en iyi şey gibi görünüyor (Glenn maynard'ın yanıta göre):

try:
  ProcessFile(filePath)
except Exception as e:
  raise Exception('Failed to process file'   filePath, e), None, sys.exc_info()[2]

Bu mesajlar hem tracebacks hem de içerir, ama traceback nerede oluştu görünmüyor.

CEVAP
5 HAZİRAN 2011, Pazar


Python 3'te aşağıdakileri yapabilirsiniz:

try:
    raise MyExceptionToBeWrapped("I have twisted my ankle")

except MyExceptionToBeWrapped as e:

    raise MyWrapperException("I'm not in a good shape") from e

Böyle bir şey üretir:

Traceback (most recent call last):
...
MyExceptionToBeWrapped: ("I have twisted my ankle")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
...
MyWrapperException: ("I'm not in a good shape")

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • CruzerLite

    CruzerLite

    1 EKİM 2011
  • Dom Esposito

    Dom Esposito

    26 Mayıs 2011
  • ODN

    ODN

    26 Kasım 2006