&; İç ö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
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")
özel durum "ben İllegalStateExcep...
Python: HERHANGİ bir özel durum yakala...
&Quot;özel Durum" kes; Aracı var m...
((yeni nesne) kilit -- Kargo kültü ya ...
Bir iş parçacığı'arayan iş parçac...