digitalmars.D - Exception safety
- Sergey Gromov (27/27) Dec 12 2008 What happens if an extern(Windows) function throws?
What happens if an extern(Windows) function throws? Basically I want my export DLL interface functions to fail gracefully. What happens if I don't catch? Currently my typical export looks like this, and it looks a bit verbose: extern(Windows) export HRESULT DllRegisterServer() { try { doStuff(); } catch(Exception e) { myTrace("Shit happens. Reason: " ~ e.msg); return E_UNEXPECTED; } catch(Object obj) { myTrace("Shit happens. Reason: " ~ obj.toString()); return E_UNEXPECTED; } catch { myTrace("Shit happens. Reason: unknown"); return E_UNEXPECTED; } return S_OK; }
Dec 12 2008
Sergey Gromov wrote:What happens if an extern(Windows) function throws?Very, very bad things.Basically I want my export DLL interface functions to fail gracefully. What happens if I don't catch? Currently my typical export looks like this, and it looks a bit verbose:Yup. It's pretty awful, and you need to remember to do it every time. I do something like: void doDllRegisterServer() { doStuff(); } extern(Windows) export HRESULT DllRegisterServer() { mixin(SafeInvoke("doDllRegisterServer()")); } where SafeInvoke creates a string mixin which adds all the catch wrappers.extern(Windows) export HRESULT DllRegisterServer() { try { doStuff(); } catch(Exception e) { myTrace("Shit happens. Reason: " ~ e.msg); return E_UNEXPECTED; } catch(Object obj) { myTrace("Shit happens. Reason: " ~ obj.toString()); return E_UNEXPECTED; } catch { myTrace("Shit happens. Reason: unknown"); return E_UNEXPECTED; } return S_OK; }
Dec 12 2008
Reply to Sergey,What happens if an extern(Windows) function throws? Basically I want my export DLL interface functions to fail gracefully. What happens if I don't catch? Currently my typical export looks like this, and it looks a bit verbose: extern(Windows) export HRESULT DllRegisterServer() { try { doStuff(); } catch(Exception e) { myTrace("Shit happens. Reason: " ~ e.msg); return E_UNEXPECTED; } catch(Object obj) { myTrace("Shit happens. Reason: " ~ obj.toString()); return E_UNEXPECTED; } catch { myTrace("Shit happens. Reason: unknown"); return E_UNEXPECTED; } return S_OK; }you can burry two of those in a function and getextern(Windows) export HRESULT DllRegisterServer() { try { doStuff(); } catch(Exception e) { return OhCrud(e); } catch{ return OhCrud(); } return S_OK; }
Dec 12 2008