The best option here is to have just one exception block:
DECLARE
exc1 EXCEPTION;
i integer:='1';
BEGIN
IF i = 1 THEN
RAISE exc1;
END IF;
EXCEPTION
WHEN exc1 THEN
raise_application_error(-20001, 'test123');
WHEN OTHERS THEN
raise_application_error(-20481,
'Error while inserting/update into Table- ' ||
SQLERRM);
END;
/
But if you really need nested anonymous block with own exceptions blocks, you need to add one more exception handler:
DECLARE
exc1 EXCEPTION;
i integer:='1';
BEGIN
BEGIN
IF i = 1 THEN
RAISE exc1;
END IF;
EXCEPTION
WHEN exc1 THEN
raise;
WHEN OTHERS THEN
raise_application_error(-20481,
'Error while inserting/update into Table- ' ||
SQLERRM);
END;
EXCEPTION
WHEN exc1 THEN
raise_application_error(-20001, 'test123');
END;
/
PS. I'd suggest you do not use when others then without adding original exception into the error stack, ie 3rd parameter of raise_application_error() should be true:
DECLARE
exc1 EXCEPTION;
i integer:='1';
BEGIN
IF i = 1 THEN
RAISE exc1;
END IF;
EXCEPTION
WHEN exc1 THEN
raise_application_error(-20001, 'test123', true);
WHEN OTHERS THEN
raise_application_error(-20481,
'Error while inserting/update into Table- ' ||
SQLERRM
, true);
END;
/
OTHERSexception handler is being executed is because the exc1 handler is not in scope. when yourifblock executes its true code.