To understand your issues it would be helpful if you could share your code.
But it is not possible to define the exit handler within the loop directly. I has to be defined at the beginning of e.g. a block. You could change your code, that within your loop you call a further procedure which does the handling of a single data set. Within that further procedure you can define your exit handler which inserts the data set in the log table with the help of an autonomous transaction in case your insert into the "main" table fails.
Best Regards,
Florian