Oracle will throw an exception saying:
ORA-02290: validation violation (yourschema.valid_gender) violated
You can catch this in the exception handler and create your own exception using raise_application_error several ways.
1) You can specifically block the ORA-02290 exception as follows:
declare e_check_violated exception pragma exception_init (e_check_violated, -2290); begin insert ... exception when e_check_violated then if sqlerrm like '%(yourschema.valid_gender)%' then raise_application_error(-20001,'Invalid gender'); else raise; end if; end;
2) You can catch all the exceptions and check them:
begin insert ... exception when others then if sqlerrm like 'ORA-02290:%(yourschema.valid_gender)%' then raise_application_error(-20001,'Invalid gender'); else raise; end if; end;
In a large application, it quite often happens that the exception handling procedure generalizes this and looks at the restriction-related message in the table.
Tony Andrews
source share