www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Exception safety

reply Sergey Gromov <snake.scaly gmail.com> writes:
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
next sibling parent Don <nospam nospam.com> writes:
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
prev sibling parent BCS <ao pathlink.com> writes:
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 get
 extern(Windows) export HRESULT DllRegisterServer()
 {
 try
 {
 doStuff();
 }
 catch(Exception e) { return OhCrud(e); }
 catch{ return OhCrud(); }
 return S_OK;
 }

Dec 12 2008