I'm converting Oracle to PostgreSQL, I haven't alternative for SQLERRM( SQLCODE ) like Oracle in PostgreSQL.
DECLARE
name employees.last_name%TYPE;
v_code NUMBER;
v_errm VARCHAR2(64);
BEGIN
SELECT last_name INTO name FROM employees WHERE employee_id = 1000;
EXCEPTION
WHEN OTHERS THEN
v_code := SQLERRM( SQLCODE );
v_errm := SUBSTR(SQLERRM, 1 , 64);
DBMS_OUTPUT.PUT_LINE('The error code is ' || v_code || '- ' || v_errm);
END;
OTHERS. Catch the exceptions you expect to occur (such asNO_DATA_FOUNDorTOO_MANY_ROWS) and then let the code fail on the unexpected errors so that it can be debugged and so that any subsequent code does not have expectations that will not have been met if this code silently fails.