os_generate_error and postmortems
Jonathan Coxhead
jonathan at doves.demon.co.uk
Tue Aug 24 20:15:38 BST 2004
Either ...
You can catch the error by writing a signal handler for SIGOSERROR, and in
the handler you can do whatever you want, including longjmp() to some higher
stack frame. The x library in OSLibSupport shows you how to do this. It's hard
to get right, and not for the novice.
Or ...
You can use the error-returning version instead of the error-generating
version, and write the code yourself ...
os_error err = {0, "foobar"};
xwimp_report_error (xos_generate_error (&err), ...);
(In this case, xos_generate_error() is pretty much a NOP---it just returns
its argument, so this is the same as
xwimp_report_error (&err, ...);
)
By far the best approach to this whle issue is to structure your application
like this:
(*) Whenever you call a SWI or any error-returning function, check the error
return, and "goto finish" immediately if there was an error.
if ((err = f()) != NULL)
goto finish;
(*) Write all your functions to return an error
os_error *g (...)
{
os_error *err = NULL;
/* code code code, as above */
finish:
return err;
}
(*) At the top level of the app, decide what you want to do with errors that
you get:
int main()
{
os_error *err = NULL;
/* body of code */
if (err != NULL)
xwimp_report_error (err, ...);
return 0;
}
This is the "C way" of coding up C++-style exceptions. When an error
happens, the function that called the SWI returns, straight away. Then the
function that called *it* returns, and so on, all the way back up the call stack
until we finally get to the place where the error is handled. In other words,
the stack is unwound until a handler is found---just like a "real" exception.
This lets you separate error-generation from error-handling in a very
flexible way.
Hope this helps ...
Stefan Bellon wrote:
> Hi!
>
> I'm stumbling over the fact that os_generate_error() always produces a
> postmortem with stack trace which I don't want. How should I do it if I
> only want an error message (printed on screen when from within the CLI
> or in an error box when from within the WIMP) and a return code?
>
> My code looks as follows:
>
> #include <oslib/os.h>
>
> int main(void)
> {
> os_error err = { 0, "foobar" };
> os_generate_error(&err);
> }
>
> And then the following happens:
>
> *cc -IOSLib: err.c
> Norcroft RISC OS ARM C vsn 5.55 [17 Oct 2003]
> "c.err", line 2: Warning: Non-ANSI #include <oslib/os.h>
> c.err: 1 warning, 0 errors, 0 serious errors
> *err
>
> foobar
>
> Postmortem requested
> Arg2: 0x00008080 32896 -> [0xe52de004 0xe24ddc01 0xe1a0000d
> 0xe59f1040]
> Arg1: 0x0000aae4 43748 -> [0x00727265 0x0000aae8 0x3afffff2 00000000]
> 80c8 in shared library function
> 812c in anonymous function
>
> What am I missing here? This is with the latest OSLib 6.70.
>
> Thanks a lot in advance!
>
> Greetings,
> Stefan.
>
--
/| Jonathan Coxhead
o o o (_|/ 660 Gail Ave #A3
/| Sunnyvale CA 94086-8160
(_/ tel: +1 (408) 245-5285
More information about the oslib-user
mailing list