Oracle guarantees that only one error will be returned per call to OCIErrorGet() (note the singular):
Returns an error message in the provided buffer and an Oracle database error code.
...
Multiple diagnostic entries can be obtained by calling OCIErrorGet () several times until there are no more entries (OCI_NO_DATA is returned). OCIErrorGet () returns no more than one diagnostic record.
Whether you need a similar loop or not depending on what (PL /) SQL code you call. Simple SQL statements usually return only one error code; eg,
SQL> select 1/0 from dual; select 1/0 from dual * ERROR at line 1: ORA-01476: divisor is equal to zero
However, if PL / SQL is involved, more can be returned:
SQL> begin 2 raise_application_error(-20000, 'error'); 3 end; 4 / begin * ERROR at line 1: ORA-20000: error ORA-06512: at line 2
Here, the actual error that interests you will be ORA-20000. Oracle exception throwing runs from the inner block to the outer block, so assuming you are not dealing with compilation errors, the first cause of the error will be the first exception. If you catch and throw an exception, this changes. An example provided by Oracle in the docs:
SQL> begin 2 dbms_output.put_line(1/0); -- handled 3 exception when zero_divide then 4 dbms_output.put_line(1/0 || ' is undefined'); -- not handled 5 end; 6 / begin * ERROR at line 1: ORA-01476: divisor is equal to zero ORA-06512: at line 4 ORA-01476: divisor is equal to zero
DBMS_OUTPUT.PUT_LINE - procedure, so the same exception appears twice; note that this is still the first exception that interests you.
To answer your questions:
You do not need a similar loop; you should use only one if you want to get more than one error code.
If multiple errors are returned, you should probably throw the first error code because of the method in which Oracle throws exceptions; it is essentially a call to judgment that you need to make. The docs are unclear whether OCIErrorGet() returns the most recent or earliest exception; you may need to throw the last exception.