指引网

当前位置: 主页 > 数据库 > Oracle >

Oracle异常的抛出处理

来源:网络 作者:佚名 点击: 时间:2018-01-13 06:40
[摘要] --一异常处理的代码 --sqlcode 异常编号 --sqlerrm 信号字符串 /* 在plsql 块中格式 Declare 变量 Begin 代码块 EXCEPTION when 异常的名称 then 如生上面的异常时做的具体工作。 End; */ set serveroutpu
--一异常处理的代码

  --sqlcode 异常编号

  --sqlerrm 信号字符串

  /*

  在plsql 块中格式

  Declare

  变量

  Begin

  代码块

  EXCEPTION

  when 异常的名称  then

  如生上面的异常时做的具体工作。

  End;

  */

  set serveroutput on;

  create or replace procedure pr12

  as

  --定义一个int变liang

  v_age integer;

  v_name varchar(30);

  begin

  v_age:=89;

  --通过select给v_name设置值

  --修改成过程

  select name into v_name from stud where id=1;

  DBMS_OUTPUT.PUT_LINE('没有出错');

  exception

  when value_error then

  SYS.DBMS_OUTPUT.PUT_LINE('数值错误');

  when no_data_found then

  SYS.DBMS_OUTPUT.PUT_LINE('没有数据');

  when others then

  SYS.DBMS_OUTPUT.PUT_LINE(sqlcode||'你出错了'||sqlerrm);

  end;

  exec pr12();

  -----------------------------------------

  --自定义异常自己抛出异常/

  /*

  定义一个自己的异常

  myException Exception;

  抛出异常

  RAISE myException;

  处理自己的异常:

  Exception

  When myException then

  …

  */

  set serveroutput on;

  declare

  myEx exception;

  begin

  DBMS_OUTPUT.PUT_LINE('这里没错');

  raise myEx;

  DBMS_OUTPUT.PUT_LINE('不会输出,前面抛出异常');

  --处理异常

  exception

  when myEx then

  DBMS_OUTPUT.PUT_LINE('自己的异常'||sqlcode||'  '||sqlerrm);

  when others then

  DBMS_OUTPUT.PUT_LINE('不知知道什么错误'||sqlcode||sqlerrm);

  END;

  ---出错直接抛出

  declare

  begin

  DBMS_OUTPUT.PUT_LINE('no errors');

  --直接抛出

  RAISE_APPLICATION_ERROR(-20000, 'A');

  DBMS_OUTPUT.PUT_LINE('go okk…');

  exception

  when others then

  DBMS_OUTPUT.PUT_LINE(sqlcode||'  '||sqlerrm);

  end;

------分隔线----------------------------