Using max(field)+1
to generate unique values is a very bad idea. You should use a sequence for that. If you strictly need a value without gaps, you should create a table with the values and recover them from it.
What you want to do might be done easily using SQL, as other answers have already showed. Anyway, if you want to do it on PL/SQL and applying your logic stated on the question:
1.The error code for pragma is -1400 ( without the leading 0 )
2.You need to do something after the exception, either raise the error or not.
3.You need to recover the max+1 after the pragma exception to insert what you want
declare
e_insert_excep exception;
pragma exception_init(e_insert_excep,-1400);
v_depto dept.deptno%type;
begin
insert into dept values(NULL,'LOGISTICS','SINGAPORE');
commit;
exception
when e_insert_excep then
select max(deptno)+1 into v_depto from dept;
-- again the insert
insert into dept values(v_depto,'LOGISTICS','SINGAPORE');
commit;
null; -- I don't want to raise an error, therefore I set null;
when others then
dbms_output.put_line(sqlerrm);
rollback; -- In case you want to rollback any dml done ( not necessary in your case, though )
raise; -- I raised because of other error
end;
Example
SQL> create table x ( deptno number not null , c1 varchar2(20) , c2 varchar2(20) ) ;
Table created.
SQL> insert into x values ( 1 , 'XX' , 'YY' ) ;
1 row created.
SQL> commit ;
Commit complete.
SQL> declare
n_null exception;
pragma exception_init(n_null,-1400);
v_depto x.deptno%type;
begin
insert into x values(NULL,'LOGISTICS','SINGAPORE');
commit;
exception
when n_null then
-- select max value
select max(deptno)+1 into v_depto from x;
insert into x values(v_depto,'LOGISTICS','SINGAPORE');
commit;
null; -- if I don't want an error, I set null;
when others then
-- dbms_output.put_line(sqlerrm);
rollback; -- In case you want to rollback any dml done ( not necessary in your case, though )
raise; -- I raised because of other error
end;
/
PL/SQL procedure successfully completed.
SQL> select * from x ;
DEPTNO C1 C2
---------- -------------------- --------------------
1 XX YY
2 LOGISTICS SINGAPORE
SQL>