From RTSC-Pedia
[printable version] | offline version generated on 02-Oct-2009 22:10 UTC |
Using xdc.runtime Errors/Example 3
Creating a custom error handler
Contents |
Creating a Custom Error Handler
In this example we show how to create and "install" a custom error handler. Each error raised by an application is optionally passed to a supplied user supplied function. Once an error handler function is created, it is "installed" by setting the Error.raiseHook configuration parameter to point to this function.
In the example below, we start with essentially the same application as Error Example 1 and add an "error hook" function named errHook().
|
|
The errHook() function, while not very practical, illustrates the sorts of things that can be tested within an error handler. In particular, it is possible to
- test which module originally raised the error 1,
- test for and specially "handle" specific errors 2, and
- display or log the error 3.
Every RTSC module Mod provides a Mod_Module_id() macro that returns a unique module ID. On line 1 above, we show how this ID can be retrieved and used at runtime to conditionally affect the behavior of the application based on which module raised the error. In this case, Main_Module_id() returns the ID of the xdc.runtime.Main module.
Running the application above results in the following output.
| error 28: app error: generic: xdc.runtime.Main: "hook.c", line 66: generic error: num must be even xdc.runtime.Error.raise: terminating execution |
Comparing the output of this example with that of Error Example 1 we see that errHook()
- outputs the error code specified by the user in the configuration script, "error 28:";
- properly detected that the error occurred in the main application and output "app error:";
- determined that the type of error was Error.E_generic and displayed "generic:", and finally
- called the "normal" error print function to output the same information displayed in Example 1.
Error codes can be individually set in configuration script but it's also possible to find and set all error codes in an application or check that every error has a code that has been set.
| var mods = xdc.om.$modules; for (var i = 0; i < mods.length; i++) { var M = mods[i]; for (var f in M) { if (M[f] instanceof Error.Id) { var err = M[f]; err.code = 17; print("M.$name + "." + f + ".code = " + err.code); } } } |
See also
Using xdc.runtime Errors/Example 1 | How to use the Error module |
Using xdc.runtime Errors/Example 2 | How to use the Assert module |
[printable version] | offline version generated on 02-Oct-2009 22:10 UTC |