Your program should use the IKJEFF18 service routine to generate fairly reasonable error messages after a dynamic allocation error (reg 15 is not equal to 0). IKJEFF18 is a TSO routine and is documented with TSO, but it has an option to return one or two messages in a buffer that your program can then print; when called in this fashion it runs perfectly in a batch environment. The TS did not state he was using dynamic allocation in the initial post; as I read it the TS did not know about dynamic allocation.
By the way, "To enrico's post I would like to reply that for both the conditions (already existing dataset and insufficient space) the dynamic allocation routine returns same RC" is basically meaningless; the return codes are not the same. Yes, reg 15 is the same, but S99ERROR and S99INFO in the dynamic allocation request block are different. Another issue: "insufficient space" has multiple meanings; many cases of "insufficient space" cannot be detected until you open and write to the dataset; allocation cannot detect this problem.
Another issue for the TS: a field name like x_retcode means nothing to most of us; Enrico is better than me at guessing these things. Use standard field names like S99ERROR. The format of dynamic allocation data areas is in the IEFZB4D0 macro: use this macro when you do dynamic allocation.
If you are not certain if the dataset already exists, you should attempt the allocation twice; first to allocate an existing cataloged dataset and then to allocate a new dataset if the first allocation fails.