The X server reports protocol errors at the time that it detects them. If more than one error could be generated for a given request, the server can report any of them.
Because Xlib usually does not transmit requests to the server immediately (that is, it buffers them), errors can be reported much later than they actually occur. For debugging purposes, however, Xlib provides a mechanism for forcing synchronous behavior (see "Enabling or Disabling Synchronization"). When synchronization is enabled, errors are reported as they are generated.
When Xlib detects an error, it calls an error handler, which your program can provide. If you do not provide an error handler, the error is printed, and your program terminates.