I’ve been working on an experimental library that implements Try/Catch. I started with the CException library. There are some serious caveats to such a thing, since it’s not really possible to unwind the stack as C++ exceptions would. That said, for a more advanced user, it could be useful. The API is the same as CException, plus a few things specific to implementing for Particle (and minus a few things, too, such as the separate config include)
Perhaps more interestingly, the library also includes an experimental hardware fault handler that allows a hardware exception to be “caught” as well. Obviously in many cases there won’t be enough functional system left by the time a hardware exception occurs to have anything to throw the exception to or to be able to actually call the logger, but in many other cases there could be. If the fault can be recovered from, the library logs the hardware exception data and throws an exception. The handler must be activated by calling CEXCEPTION_ACTIVATE_HW_HANDLERS()
, either in setup()
or somewhere in STARTUP()
.
Currently it only works on threaded platforms. On threaded platforms, creating a new thread that will participate in exception handling requires using the provided NEW_THREAD()
and END_THREAD()
macros to properly register the thread with the library. The NEW_THREAD()
parameter list is the same as the os_thread_create()
parameters, plus one more at the end for an optional unhandled exception callback; if an unhandled exception occurs in a thread created with the NEW_THREAD() macro, the thread will be terminated and optionally a callback called. Threads created with new Thread()
cannot currently participate in exception handling. By default, the library is set up for one thread. To register more threads, first call CEXCEPTION_SET_NUM_THREADS(num)
.
I’m working on adding some interesting examples (and some detail to the one that’s there already). There is a full test suite (uses unit-test.h from the firmware tests). (I’ve only tested against Photon at the moment)
Lastly, the library requires at least firmware 0.6.0-rc1 because it uses the new logging feature.
CException-Particle Library on GitHub
At this point the library is experimental…but if this sort of thing interests you, feedback is welcome! I haven’t yet set it up as a Web IDE library, so to mess around you’ll have to build local. If/when it’s a little less experimental, I will add it.
Also here’s a rather windy article I wrote while working through some of the gritty hardware details. It talks about how the hardware exception handling works.
ARM Cortex M3: Recovering from a Hard Fault.md