From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10f8a1,e7e13c943c611b62 X-Google-Attributes: gid10f8a1,public X-Google-Thread: 102b75,be3d6a6131c61fa7 X-Google-Attributes: gid102b75,public X-Google-Thread: 1164f8,be3d6a6131c61fa7 X-Google-Attributes: gid1164f8,public From: zalman@netcom.com (Zalman Stern) Subject: Re: Exception handling Date: 1999/04/24 Message-ID: #1/1 X-Deja-AN: 470475107 Sender: zalman@netcom15.netcom.com References: <36E9F2A0.9A401A09@pacific.net.sg> <924046418.053.46@news.remarQ.com> Organization: ICGNetcom Followup-To: comp.arch,comp.sys.dec,vmsnet.alpha Newsgroups: comp.arch,comp.sys.dec,vmsnet.alpha Daniel P. B. Smith (dpbsmith@world.std.com) wrote: : In my case: I write "front-end" code; the code calls a library, which is : provided by the company we are OEM-ing from; the library in turn makes : callbacks to "back-end" routines that I provide. The library is : documented as if were written in C, and their code examples are in C. : But, silly me, I thought that backward compatibility with C was almost : C++'s whole reason for being, and that I was reasonably safe mixing and : matching as long as I used extern "C" directives properly in my headers. : I _did_ wonder why the program crashed whenever one of my callback : routines threw an exception... You must wrap all callback routines with code that catches all exceptions and turns them into error returns. This should be obvious in the case of a callback routine that returns an error code. The compiler could not possibly automagically turn a thrown exception into the right kind of error code return. You can experiment with writing templates to do some of the wrapper work for you, but its probably not worthwhile. (Unless you know you have a design that works and know all the C++ compilers you're targeting have decent template support.) Probably better to either write a script to preprocess the code or just do it by hand if there aren't many routines. The binary compatibility gotcha is a big lose. It means that even if folks could be convinced to use a richer calling convention for standard system services and libraries (which I doubt will happen), different compilers on the same platform would have to be modified to conform to the same binary calling convention. Another place where callback functions to C libraries lose is in type checking and C++ style. In C++ you probably want an object which has a method invoked when the callback is called. Once again, you can often get this effect in a wrapper. On the other side, you're going to need wrappers to call into the library which check the error code and possibly throw an exception. By the way, I don't advocate this technique in all situations. It really depends on the details of the application and how much code lives on each side of the abstraction boundaries. -Z-