On 2/15/21 1:04 PM, Jack wrote:

> I have to make my function nothrow because the function that calls it
> (not written by me) is nothrow. So I need to wrap my code in a
> try-catch() but how will I report the error message, if the toString()
> from Throwable isn't nothrow? how do I get out this circular dependence?

I understand that the caller is not written by you but I hope it at least expects an 'int' code from f(). The following "last error" is a common way for no-exception languages like C. I put the fprintf in g() but you can put it at a higher level that you control. You can print fLastError when g() fails.

string fLastError;

int f() nothrow
{
  import std.conv : to;
  fLastError = null;

  try {
    "hi".to!int;

  } catch(Throwable th) {
    import std.format;
    fLastError = __FUNCTION__ ~ " failed: " ~ th.msg;
    return 1;
  }

  return 0;
}

int g() nothrow {
  const err = f();
  if (err) {
    import core.stdc.stdio : fprintf, stderr;
    fprintf(stderr, "%.*s\n", cast(int)fLastError.length, fLastError.ptr);
  }

  return err;
}

int main() {
  return g();
}

Ali

Reply via email to