是說存儲過程嗎?create procedure *************** as except1 exception; begin **** raise except1; **** exception when except1 then ****** when others then ****** end;
存储过程中可以定义异常 create or replace procedure myprocedure as myexception exception; --定义异常类型变量 begin if 1>0 then raise myexception; --出现异常时抛出异常 end if; exception when myexception then --定义异常时处理的语句 dbms_output.put_line('出现异常');end;
用户自定义异常 :pl/sql程序中有程序员自己定义的异常,他们是正在编写程序代码的应用程序所专有的。1声明 exception_name Exception;使用例子 declare exp_001 exception; begin ... end; 2使用raise语句引发用户自定义异常 raise exception_name; declare exp_001 exception; begin ... end;
declare exp_001 exception; v_a varchar2(20); begin ... if v_a is null then raise exp_001; end if; exception when exp_001 then dbms_output.put_line('v_a 不能为空'); raise; end;
set serveroutput on declare Exception01400 EXCEPTION; -- 初始化异常 PRAGMA EXCEPTION_INIT(Exception01400, -1400); begin insert into Excel_BaseCode(CodeNO) values (null); exception when Exception01400 then dbms_output.put_line('主键不能为空'); end;
declare go_excep EXCEPTION; --声明异常变量 unique_error EXCEPTION; pragma exception_init(unique_error, -00001); --给异常变量赋值 pragma exception_init(go_excep, -20002); begin begin if length('fg') > 0 then raise_application_error(-20002, '长度大于零'); --抛出-20002,长度大于零的异常 else raise go_excep; --抛出go_excep异常 end if; end; begin insert into mw_app.mwt_userinfo values ('', '', ''); end; exception when go_excep then dbms_output.put_line('自定义异常!'); dbms_output.put_line('sqlcode:' || sqlcode); dbms_output.put_line('sqlerrm:' || sqlerrm); when unique_error then dbms_output.put_line('违反唯一约束!'); WHEN OTHERS THEN ROLLBACK; end; 这是我以前写的异常处理的一个例子 还有 自定义异常是有范围的 好像是-20000到-20300之间 楼主可以上网搜一下
except1 exception;
begin
****
raise except1;
****
exception
when except1 then
******
when others then
******
end;
create or replace procedure myprocedure
as
myexception exception; --定义异常类型变量
begin
if 1>0 then
raise myexception; --出现异常时抛出异常
end if; exception
when myexception then --定义异常时处理的语句
dbms_output.put_line('出现异常');end;
:pl/sql程序中有程序员自己定义的异常,他们是正在编写程序代码的应用程序所专有的。1声明
exception_name Exception;使用例子
declare
exp_001 exception;
begin
...
end; 2使用raise语句引发用户自定义异常
raise exception_name;
declare
exp_001 exception;
begin
... end;
和上面2位的一样
when 后面祥和自己的处理语句
谁能帮写一个自定义错误列型 比如ORA-01400错误的例子啊
输出"主键不能为空"
exp_001 exception;
v_a varchar2(20);
begin
...
if v_a is null then
raise exp_001;
end if;
exception
when exp_001 then
dbms_output.put_line('v_a 不能为空');
raise;
end;
declare
Exception01400 EXCEPTION;
-- 初始化异常
PRAGMA EXCEPTION_INIT(Exception01400, -1400);
begin
insert into Excel_BaseCode(CodeNO) values (null);
exception
when Exception01400 then
dbms_output.put_line('主键不能为空');
end;
go_excep EXCEPTION; --声明异常变量
unique_error EXCEPTION;
pragma exception_init(unique_error, -00001); --给异常变量赋值
pragma exception_init(go_excep, -20002);
begin
begin
if length('fg') > 0 then
raise_application_error(-20002, '长度大于零'); --抛出-20002,长度大于零的异常
else
raise go_excep; --抛出go_excep异常
end if;
end;
begin
insert into mw_app.mwt_userinfo values ('', '', '');
end;
exception
when go_excep then
dbms_output.put_line('自定义异常!');
dbms_output.put_line('sqlcode:' || sqlcode);
dbms_output.put_line('sqlerrm:' || sqlerrm);
when unique_error then
dbms_output.put_line('违反唯一约束!');
WHEN OTHERS THEN
ROLLBACK;
end;
这是我以前写的异常处理的一个例子
还有 自定义异常是有范围的 好像是-20000到-20300之间 楼主可以上网搜一下