[zeromq-dev] Questions about Coding Style

john skaller skaller at users.sourceforge.net
Sun Feb 12 07:39:40 CET 2012

On 12/02/2012, at 11:40 AM, Gary Wright wrote:

> On Feb 11, 2012, at 4:53 PM, Pieter Hintjens wrote:
>> So in my libraries, like CZMQ, I validate input arguments with
>> asserts, systematically.
> And yet for some language bindings it would be more idiomatic to
> raise an exception, rather than just have the process terminate. If
> libzmq returned an error code, the language binding could 'do
> the right thing'.

That's a good point.

Actually asserts() are used for two distinct purposes:

(1) a bug in ZMQ

(2) a bug in the parameters or environment 
   (a) null pointer
   (b) memory corruption detected

There may be more, eg out of memory, I don't know.

A bug in ZMQ should crash the process. The "right thing" is not
really so important (other than providing some help to find the bug).

However the other cases might warrant an exception, especially
in a long running server which just sloppy validating a client 
module .. you may want to kill the module, but not the server.

It's not clear an error handler would actually allow an immediate return with
an error code, but it *could* throw an exception. [The assert could be 
buried deeply in function calls that can't return error codes]

And the a binding could catch it and then do what is required.

For example, in the C binding, a C++ "null pointer exception" thrown could be
caught and translated into something else, eg EINVAL return code.
It seems risky: ZMQ specifies in the Coding Style that no exceptions should
be thrown. This probably means the code doesn't try to be exception safe.
In turn that means returning an error code is risky, even for a language binding.

Gary, you have an actual use case?

john skaller
skaller at users.sourceforge.net

More information about the zeromq-dev mailing list