我想捕获一个特定的异常并相应地处理它 - 然后我想继续并执行其他异常必须执行的泛型处理.
来自C背景,我以前可以使用gotos来达到预期的效果.
这是我目前正在做的,它工作正常:
try:
output_var = some_magical_function()
except IntegrityError as zde:
integrity_error_handling()
shared_exception_handling_function(zde) # could be error reporting
except SomeOtherException as soe:
shared_exception_handling_function(soe) # the same function as above
Run Code Online (Sandbox Code Playgroud)
即 - 是否有"Pythonic"方式执行以下操作:
try:
output_var = some_magical_function()
except IntegrityError as zde:
integrity_error_handling()
except ALLExceptions as ae: # all exceptions INCLUDING the IntregityError
shared_exception_handling_function(ae) # could be error reporting
Run Code Online (Sandbox Code Playgroud)
注意:我知道finally子句 - 这不是为了整理(即关闭文件)·
我使用的代码非常类似于以下Stack Overflow问题: 在PInvoke DLL'coredll.dll'中找不到入口点'GetDeviceUniqueID'
(为了后人的缘故,我的代码粘贴在这里):
[DllImport("coredll.dll")]
private extern static int GetDeviceUniqueID([In, Out] byte[] appdata,
int cbApplictionData,
int dwDeviceIDVersion,
[In, Out] byte[] deviceIDOuput,
out uint pcbDeviceIDOutput);
public static string GetDeviceID()
{
string appString = "MyApp";
byte[] appData = new byte[appString.Length];
for (int count = 0; count < appString.Length; count++)
{
appData[count] = (byte)appString[count];
}
int appDataSize = appData.Length;
byte[] DeviceOutput = new byte[20];
uint SizeOut = 20;
int i_rc = GetDeviceUniqueID(appData, appDataSize, 1, DeviceOutput, out SizeOut);
string idString = "";
for …Run Code Online (Sandbox Code Playgroud)