Search code examples
cdlldartdart-native-extension

How to throw a custom error from a dart extension dll?


I am trying to create an extension to wrap an existing DLL without extensive knowledge of C/++. I have used the sample extension as a base and everything seems to work fine, what I would like to do is have some error handling inside my dll.

Is there a way of sending custom errors back to dart if something inside the dll fails? Would it just be a case of sending lets say an array with the first parameter being a bool as to whether it failed or not and the second parameter being a string for the error if there is one. Or is there an actual way to throw errors from the dll itself?

Hope this made sense, Thanks,


Solution

  • You should take a look inside dart_api.h file, it contains a lot of comments about Dart native stuff.

    I have found Dart_ThrowException function there, but also a comment saying that Dart_NewUnhandledExceptionError should be used instead.

    Both functions need a Dart exception object handle. It seems that Dart team uses their own Dart Util library to create them:

    Dart_ThrowException(DartUtils::NewDartArgumentError("error message"))