-----------------------------------------------------------------
-- PL/SQL: Exception Handling example (Section 4.10)            -
-- Chapter 4; Oracle Programming -- A Primer                    -
--            by R. Sunderraman                                 -
-----------------------------------------------------------------

create or replace procedure insert_odetails
  (onum in integer, pnum in integer, qty in integer) as

  invalid_quantity EXCEPTION;
begin
  if (qty <= 0) then
    raise invalid_quantity;
  end if;

  insert into odetails values (onum,pnum,qty);

  exception
    when dup_val_on_index then
      dbms_output.put_line('PRIMARY KEY VIOLATION'); 
      dbms_output.put_line(SQLCODE || '--' || SQLERRM);

    when invalid_quantity then
      dbms_output.put_line('Quantity is invalid'); 
     
    when others then
      dbms_output.put_line('OTHER ERROR'); 
      dbms_output.put_line(SQLCODE || '--' || SQLERRM);
end; 
/
