Skip to main content
← PL/SQL Topics

Exception Handling

Explain handling an exception inside a loop so processing continues

Handling an Exception Inside a Loop

ExplanationOracle Fusion / EBS · Technical · PL/SQLHigh confidenceGenerated

Wrap the loop body in its own BEGIN/EXCEPTION/END so one bad row is logged and the loop moves on.

How it works

If the EXCEPTION section is on the outer block, the first error ends the whole loop. Put a nested block around the per-iteration work: its handler catches the failure for that row, records it, and control returns to the loop for the next iteration. This is the row-by-row 'reject and continue' pattern. For bulk DML the equivalent is FORALL ... SAVE EXCEPTIONS. Be deliberate about whether a partial run should COMMIT.

How it works
FOR r IN (SELECT * FROM staging_invoices) LOOP
  BEGIN
    load_one_invoice(r);                 -- may raise
  EXCEPTION
    WHEN OTHERS THEN
      INSERT INTO staging_errors(row_id, err, logged_at)
      VALUES (r.row_id, SQLERRM, SYSTIMESTAMP);
  END;
END LOOP;
COMMIT;

Related questions

GroundingGenerated

Model-generated, grounded against the curated knowledge layer. Check specifics against your instance.

Have a follow-up, or a different question?

Continue in Ask Oracle AI