create or replace procedure up_exceptions5(a number)

is 

 v_sal number;

begin

 select sal into v_sal

 from emp

 where empno = a;


 if v_sal < 1000 then

   RAISE_APPLICATION_ERROR('-20001', '급여가 너무 적사옵니다, 주인님!');

 end if;

   

  p('잘 처리되었사옵니다, 주인님!');

end;

/


exec up_exceptions5(7369)      --> 에러: ORA-20001: 급여가 너무 적사옵니다, 주인님!


↓ ↓ ↓ 


create or replace procedure up_exceptions5(a number)

is 

 v_sal number;

 e_too_small exception;

 pragma exception_init(e_too_small, -20001);

begin

 select sal into v_sal

 from emp

 where empno = a;


 if v_sal < 1000 then

   RAISE_APPLICATION_ERROR('-20001', '급여가 너무 적사옵니다, 주인님!');

 end if;

  

  p('잘 처리되었사옵니다, 주인님!');

exception

 when e_too_small then

   p('기본 급여가 너무 적사옵니다, 주인님!');

end;

/


exec up_exceptions5(7369) 




# 문제


drop table t1 purge;

create table t1 (no number primary key);

insert into t1 values (1000);

commit;


create or replace procedure up_exception_test(a number)

is

begin

 insert into t1 values (a);

end;


exec up_exception_test(1000)



[2] 방법으로 exception 처리


create or replace procedure up_exception_test(a number)

is

 예외 변수 선언;

 예외 변수를 예외 코드와 연결;

begin

 insert into t1 values (a);

exception

 when 예외 변수 then

   p('컬럼의 primary key 제약 조건을 위배하셨사옵니다, 주인님!');

end;


exec up_exception_test(1000);



[3] 방법으로 exception 처리


create or replace procedure up_exception_test(a number)

is

begin

 insert into t1 values (a);

exception

 when others then

   p('알 수 없는 예외가 발생하였사옵니다, 주인님!');

   p('예외 코드는 ' || sqlcode || '이옵고,');

   p('예외 메시지는 ' || sqlerrm || '이옵나이다.');

end;


exec up_exception_test(1000);


+ Recent posts