Skip to content

PRAGMA EXCEPTION_INIT fails with duplicate declaration error #5

@rophy

Description

@rophy

PRAGMA EXCEPTION_INIT Grammar Bug - Incorrect Use of decl_varname

Summary

PRAGMA EXCEPTION_INIT fails with "duplicate declaration" error when trying to associate an already-declared exception with an error code. This is caused by incorrect grammar production using decl_varname instead of any_identifier.

Test Case

DECLARE
  zero_excep exception;
  PRAGMA exception_init(zero_excep, -1476);
  var1 integer;
BEGIN
  var1 := 100 / 0;
exception
  WHEN zero_excep THEN
     raise info 'handle zero excep';
end;
/

Expected Behavior (Oracle 23c)

handle zero excep

PL/SQL procedure successfully completed.

Actual Behavior (IvorySQL)

ERROR:  duplicate declaration at or near "zero_excep"
LINE 3:   PRAGMA exception_init(zero_excep, -1476);
                                ^

Root Cause

In src/pl/plisql/src/pl_gram.y, the PRAGMA EXCEPTION_INIT production incorrectly uses decl_varname:

| K_PRAGMA K_EXCEPTION_INIT '(' decl_varname ',' ICONST ')' ';'

Problem: decl_varname enforces "name must NOT already be declared" and raises "duplicate declaration" error if the name exists.

But: PRAGMA EXCEPTION_INIT must reference an ALREADY declared exception variable.

This creates an impossible situation:

  1. zero_excep exception; - Declares the exception
  2. PRAGMA exception_init(zero_excep, ...) - ERROR: "duplicate declaration"!

Proposed Solution

Change the grammar to use any_identifier instead of decl_varname:

| K_PRAGMA K_EXCEPTION_INIT '(' any_identifier ',' exception_error_code ')' ';'

This allows PRAGMA EXCEPTION_INIT to reference existing exception variables without triggering duplicate declaration checks.

References

Related Issues

This blocks proper Oracle PL/SQL compatibility for user-defined exceptions with custom error codes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions