| 订阅 | 在线投稿
分享
 
 
 

全面解析Oracle developer的异常处理机制

2008-08-19 06:50:59 编辑來源:互联网 国际版 评论
 
 
本文为【全面解析Oracle developer的异常处理机制】的汉字拼音对照版显示拼音
  Oracle developeryiqikuaisudeshujuchulikaifaerwenmingqiyichangchulijizhiyeshibijiaowanshanbukexiaoqu

  1 yichangdeyoudian

  ruguomeiyouyichangzaichengxuzhongyingdangjianchameigeminglingdechenggonghuanshishibairu

  BEGIN

  SELECT ...

  -- check for no data found error

  SELECT ...

  -- check for no data found error

  SELECT ...

  -- check for no data found error

  zhezhongshixiandefangfaquedianzaiyucuowuchulimeiyouyuzhengchangchulifenkaikeduxingcha使shiyongyichangkeyifang便bianchulicuowuerqieyichangchulichengxuyuzhengchangdeshiwuluojifenkaitigaolekeduxingru

  BEGIN

  SELECT ...

  SELECT ...

  SELECT ...

  ...

  EXCEPTION

  WHEN NO_DATA_FOUND THEN -- catches all no data found errors

  2 yichangdefenlei

  youliangzhongleixingdeyichangyizhongweineibuyichangyizhongweiyonghuzidingyiyichangneibuyichangshizhixingqijianfanhuidaoPL/SQLkuaideORACLEcuowuhuoyouPL/SQLdaimademoucaozuoyinqidecuowuruchushuweilinghuoneicunyichudeqingkuangyonghuzidingyiyichangyoukaifazhexianshidingyizaiPL/SQLkuaizhongchuandixinxiyikongzhiduiyuyingyongdecuowuchuli

  meidangPL/SQLweibeileORACLEyuanzehuochaoyuelexitongyilaideyuanzejiuhuiyinshidechanshengneibuyichangyinweimeigeORACLEcuowudouyouyigehaomabingqiezaiPL/SQLzhongyichangtongguomingzichuliORACLEtigongleyudingyideneibuyichangruSELECT INTO yujubufanhuixingshichanshengdeORACLEyichangNO_DATA_FOUNDduiyuyudingyiyichangxianjiangzuichangyongdeyichangliejuruxia

  exception oracle error sqlcode value condition

  no_data_found ora-01403 +100 select into yujumeiyoufuhetiaojiandejilufanhui

  too_mang_rows ora-01422 -1422 select into yujufuhetiaojiandejiluyouduotiaofanhui

  dup_val_on_index ora-00001 -1 duiyushujukubiaozhongdemouyiliegailieyijingbeixianzhiweiweiyisuoyinchengxushitucunchulianggechongfudezhi

  value_error ora-06502 -6502 zaizhuanhuanzifuleixingjiequhuochangdushouxianshihuifashenggaiyichangruyigezifufenpeigeiyigebianliangergaibianliangshengmingdechangdubigaizifuduanjiuhuiyinfagaiyichang

  storage_error ora-06500 -6500 neicunyichu

  zero_divide ora-01476 -1476 chushuweiling

  case_not_found ora-06592 -6530 duiyuxuanzecaseyujumeiyouyuzhixiangpipeidetiaojiantongshiyemeiyouelseyujubuhuoqitadetiaojian

  cursor_already_open ora-06511 -6511 chengxushitudakaiyigeyijingdakaideyoubiao

  timeout_on_resource ora-00051 -51 xitongzaidengdaimouyiziyuanshijianchaoshi

  ruguoyaochuliweimingmingdeneibuyichangbixu使shiyongOTHERSyichangchuliqihuoPRAGMA EXCEPTION_INIT PRAGMAyoubianyiqikongzhihuozheshiduiyubianyiqidezhushiPRAGMAzaibianyishichulierbushizaiyunxingshichuliEXCEPTION_INITgaosubianyiqijiangyichangmingyuORACLEcuowumajieheqilaizheyangkeyitongguomingziyinyongrenyideneibuyichangbingqiekeyitongguomingziweiyichangbianxieyishidangdeyichangchuliqi

  zaizichengxuzhong使shiyongEXCEPTION_INITdeyufaruxia

  PRAGMA EXCEPTION_INIT(exception_name, -Oracle_error_number);

  zaigaiyufazhongyichangmingshishengmingdeyichangxialishiqiyongfa

  DECLARE

  deadlock_detected EXCEPTION;

  PRAGMA EXCEPTION_INIT(deadlock_detected, -60);

  BEGIN

  ... -- Some operation that causes an ORA-00060 error

  EXCEPTION

  WHEN deadlock_detected THEN

  -- handle the error

  END;

  duiyuyonghuzidingyiyichangzhinengzaiPL/SQLkuaizhongdeshengmingbufenshengmingyichangyichangdemingziyouEXCEPTIONguanjianziyinru

  reserved_loaned Exception

  chanshengyichanghoukongzhichuangeilezichengxudeyichangbufenjiangyichangzhuanxianggeziyichangkongzhikuaibixuzaidaimazhong使shiyongruxiadejiegouchulicuowu

  Exception

  When exception1 then

  Sequence of statements;

  When exception2 then

  Sequence of statements;

  When others then

  3yichangdepaochu

  yousanzhongfangshipaochuyichang

  1 tongguoPL/SQLyunxingshiyinqing

  2. 使shiyongRAISEyuju

  3. tiaoyongRAISE_APPLICATION_ERRORcunchuguocheng

  dangshujukuhuoPL/SQLzaiyunxingshifashengcuowushiyigeyichangbeiPL/SQLyunxingshiyinqingzidongpaochuyichangyekeyitongguoRAISEyujupaochu

  RAISE exception_name;

  xianshipaochuyichangshichengxuyuanchulishengmingdeyichangdexiguanyongfadanRAISEbuxianyushengmingledeyichangtakeyipaochurenherenheyichanglirunixiwangyongTIMEOUT_ON_RESOURCEcuowujiancexindeyunxingshiyichangchuliqinizhixujiandandezaichengxuzhong使shiyongxiamiandeyuju

  RAISE TIMEOUT_ON_RESOUCE;

  biruxiamianyigedingdanshurudeliziruodangdingdanxiaoyukucunshuliangzepaochuyichangbingqiebuhuogaiyichangchuliyichang

  DECLARE

  inventory_too_low EXCEPTION;

  ---qitashengmingyuju

  BEGIN

  IF order_rec.qty>inventory_rec.qty THEN

  RAISE inventory_too_low;

  END IF

  EXCEPTION

  WHEN inventory_too_low THEN

  order_rec.staus:='backordered';

  END;

  RAISE_APPLICATION_ERRORneijianhanshuyongyupaochuyigeyichangbinggeiyichangfuyuyigecuowuhaoyijicuowuxinxizidingyiyichangdequeshengcuowuhaoshi+1,queshengxinxishiUser_Defined_ExceptionRAISE_APPLICATION_ERRORhanshunenggouzaipl/sqlchengxukuaidezhixingbufenheyichangbufentiaoyongxianshipaochudaiteshucuowuhaodemingmingyichang Raise_application_error(error_number,message[,true,false]))

  cuowuhaodefanweishi-20,000dao-20,999cuowuxinxishiwenbenzifuchuanzuiduowei2048zijieTRUEheFALSEbiaoshishitianjia(TRUE)jincuowudui(ERROR STACK)huanshifugai(overwrite)cuowudui(FALSE)queshengqingkuangxiashiFALSE

  ruxiadaimasuoshi

  IF product_not_found THEN

  RAISE_APPLICATION_ERROR(-20123,'Invald product code' TRUE);

  END IF;

  4yichangdechuli

  PL/SQLchengxukuaideyichangbufenbaohanlechengxuchulicuowudedaimadangyichangbeipaochushiyigeyichangxianjingjiuzidongfashengchengxukongzhilikaizhixingbufenzhuanruyichangbufen,yidanchengxujinruyichangbufenjiubunengzaihuidaotongyikuaidezhixingbufenxiamianshiyichangbufendeyibanyufa

  EXCEPTION

  WHEN exception_name THEN

  Code for handing exception_name

  [WHEN another_exception THEN

  Code for handing another_exception]

  [WHEN others THEN

  code for handing any other exception.]

  yonghubixuzaidulideWHENzichuanzhongweimeigeyichangshejiyichangchulidaimaWHEN OTHERSzichuanbixufangzhizaizuihoumianzuoweiqueshengchuliqichulimeiyouxianshichulideyichangdangyichangfashengshikongzhizhuandaoyichangbufenORACLEchazhaodangqianyichangxiangyingdeWHEN..THENyujubuzhuoyichangTHENzhihoudedaimabeizhixingruguocuowuxianjingdaimazhishi退tuichuxiangyingdeqiantaokuainamechengxujiangjixuzhixingneibukuaiENDhoumiandeyujuruguomeiyouzhaodaoxiangyingdeyichangxianjingnamejiangzhixingWHEN OTHERSzaiyichangbufenWHEN zichuanmeiyoushuliangxianzhi

  EXCEPTION

  WHEN inventory_too_low THEN

  order_rec.staus:='backordered';

  replenish_inventory(inventory_nbr=>

  inventory_rec.sku,min_amount=>order_rec.qty-inventory_rec.qty);

  WHEN discontinued_item THEN

  --code for discontinued_item processing

  WHEN zero_divide THEN

  --code for zero_divide

  WHEN OTHERS THEN

  --code for any other exception

  END;

  dangyichangpaochuhoukongzhiwutiaojianzhuandaoyichangbufenzhejiuyiweizhekongzhibunenghuidaoyichangfashengdeweizhidangyichangbeichulihejiejuehoukongzhifanhuidaoshangyicengzhixingbufendexiayitiaoyuju

  BEGIN

  DECLARE

  bad_credit exception;

  BEGIN

  RAISE bad_credit;

  --fashengyichangkongzhizhuanxiang

  EXCEPTION

  WHEN bad_credit THEN

  dbms_output.put_line('bad_credit');

  END;

  --bad_credityichangchulihoukongzhizhuandaozheli

  EXCEPTION

  WHEN OTHERS THEN

  --kongzhibuhuicongbad_credityichangzhuandaozheli

  --yinweibad_credityibeichuli

  END;

  dangyichangfashengshizaikuaideneibumeiyougaiyichangchuliqishikongzhijiangzhuandaohuochuanbodaoshangyicengkuaideyichangchulibufen

  BEGIN

  DECLARE ---neibukuaikaishi

  bad_credit exception;

  BEGIN

  RAISE bad_credit;

  --fashengyichangkongzhizhuanxiang

  EXCEPTION

  WHEN ZERO_DIVIDE THEN --bunengchulibad_crediteyichang

  dbms_output.put_line('divide by zero error');

  END --jieshuneibukuai

  --kongzhibunengdaodazheliyinweiyichangmeiyoujiejue

  --yichangbufen

  EXCEPTION

  WHEN OTHERS THEN

  --youyubad_creditmeiyoujiejuekongzhijiangzhuandaozheli

  END;

  5yichangdechuanbo

  meiyouchulideyichangjiang沿yanjianceyichangtiaoyongchengxuchuanbodaowaimiandangyichangbeichulibingjiejuehuodaodachengxuzuiwaicengchuanbotingzhizaishengmingbufenpaochudeyichangjiangkongzhizhuandaoshangyicengdeyichangbufen

  BEGIN

  executable statements

  BEGIN

  today DATE:='SYADATE'; --ERRROR

  BEGIN --neibukuaikaishi

  dbms_output.put_line('this line will not execute');

  EXCEPTION

  WHEN OTHERS THEN

  --yichangbuhuizaizhelichuli

  END;--neibukuaijieshu

  EXCEPTION

  WHEN OTHERS THEN

  chuliyichang

  END

  zhixingbufenpaochudeyichangjiangshouxianchuandidaotongyikuaideyichangbufenruguozaitongyikuaideyichangbufenmeiyouchulizhegeyichangdechuliqinameyichangjianghuichuanbodaoshangyicengdeyichangbufenzhongyizhidaozuiwaiceng

  zaiyichangbufenpaochudeyichangjiangkongzhizhuandaoshangyicengdeyichangbufen

  lingwaicuowubaogaohanshuSQLCODEheSQLERRMzaiOTHERSchuliqizhongtebieyouyongyinweitamenfanhuiORACLEcuowudaimahexiaoxiruxialisuoshi

  declare

  err_num NUMBER;

  err_msg VARCHAR2(100);

  BEGIN

  ...

  EXCEPTION

  WHEN OTHERS THEN

  err_num := SQLCODE;

  err_msg := SUBSTR(SQLERRM, 1, 100);

  INSERT INTO errors VALUES (err_num, err_msg);

  END;

  

  2007-5-7 19:09:02 chakanpingyu»»»

  2007-5-7 19:09:43 oracle yichangchuli

  ~~~~youguanyuyichangchulide3gezhishidian

  1.EXCEPTION_INTbianyizhishi

  gongnengshijiangmoumingmingyichangtongmoutedingOraclecuowuguanlianqilai.

  zhuyongyonglaibuzhuomoutedingyichangcuowu,erbushitongguoOTHERSlaichuli.

  yufa:PROGMA EXCEPTION_INIT(exception_name,oracle_error_number)

  SQL>

  SQL> DECLARE

  2 expa EXCEPTION;

  3 PRAGMA EXCEPTION_INIT(expa,-6502);

  4 vn NUMBER(1);

  5 BEGIN

  6 vn:=24;

  7 EXCEPTION

  8 WHEN expa THEN

  9 dbms_output.put_line('shuzihuozhicuowu : shuzhijingdutaigao');

  10 WHEN OTHERS THEN

  11 dbms_output.put_line(SQLCODE);

  12 dbms_output.put_line(substr(SQLERRM,1,100));

  13 END;

  14 /

  shuzihuozhicuowu : shuzhijingdutaigao

  PL/SQL procedure successfully completed

  SQL>

  2.RAISE_APPLICATION_ERROR

  gongnengyonglaizidingyicuowuxiaoxi

  yufa: RAISE_APPLICATION_ERROR(error_number,error_message,[keep_errors])

  error_number:-20 000dao-20 999zhijiandezhi

  error_message:bixushaoyu512gezifu

  keep_errors:buerzhi.weizhenzexindecuowujiangbeijiadaoyicunzaidecuowuqingdanzhongruguoyicunzaidehuaweijiamorenzhizexindecuowujiangdaitidangqiandecuowuqingdan.

  SQL> BEGIN

  2 UPDATE t SET a='TEST' WHERE a='12345';

  3 IF SQL%NOTFOUND THEN

  4 raise_application_error(-20001,'nishalezhidaomeiyouzheyangdeshujuhuanqugengxin');

  5 END IF;

  6 EXCEPTION

  7 WHEN OTHERS THEN

  8 dbms_output.put_line(substr(SQLERRM,1,100));

  9 END;

  10 /

  ORA-20001: nishalezhidaomeiyouzheyangdeshujuhuanqugengxin

  PL/SQL procedure successfully completed

  SQL>

  3.yichangchuanbo

  Akezhixingbufenfashengdeyichang

  1)ruguodangqianyujukuaiyougaiyichangdechulichengxuzezhixingzhikongzhiquanjiaoyouwaicengyujukuai.

  dangqianyujukuaiyougaiyichangdechulichengxu:

  SQL> DECLARE

  2 expa EXCEPTION;

  3 expb EXCEPTION;

  4 BEGIN

  5 BEGIN

  6 RAISE expa;

  7 EXCEPTION

  8 WHEN expa THEN

  9 dbms_output.put_line('yichangAchansheng');

  10 END;

  11 EXCEPTION

  12 WHEN expb THEN

  13 dbms_output.put_line('yichangBchansheng');

  14 END;

  15 /

  yichangAchansheng

  PL/SQL procedure successfully completed

  SQL>

  2)ruguodangqianyujumeiyougaiyichangdechulichengxuzetongguowaicengyujukuaizhongchanshenggaiyichanglaichuanbogaiyichangranhoutongguowaicengyichangchulichengxuanbuzhoulaichuli.ruowaicengmeiyougaiyichangdechulichengxuzeyichangchuanbodaotiaoyonghuanjing.

  dangqianyujukuaimeiyougaiyichangdechulichengxu,waicengyujukuaizhongchanshenggaiyichangbingchuli

  SQL> DECLARE

  2 expa EXCEPTION;

  3 expb EXCEPTION;

  4 BEGIN

  5 BEGIN

  6 RAISE expb;

  7 EXCEPTION

  8 WHEN expa THEN

  9 dbms_output.put_line('yichangAchansheng');

  10 END;

  11 EXCEPTION

  12 WHEN expb THEN

  13 dbms_output.put_line('yichangBchansheng');

  14 END;

  15 /

  yichangBchansheng

  PL/SQL procedure successfully completed

  SQL>

  dangqianyujukuaimeiyougaiyichangdechulichengxu,waicengyujukuaizhongchanshenggaiyichangqiemeiyougaiyichangchulichengxuyichangchuanbodaotiaoyonghuanjing

  SQL> DECLARE

  2 expa EXCEPTION;

  3 expb EXCEPTION;

  4 BEGIN

  5 BEGIN

  6 RAISE expb;

  7 EXCEPTION

  8 WHEN expa THEN

  9 dbms_output.put_line('yichangAchansheng');

  10 END;

  11 EXCEPTION

  12 WHEN expa THEN

  13 dbms_output.put_line('yichangAchansheng');

  14 END;

  15 /

  DECLARE

  expa EXCEPTION;

  expb EXCEPTION;

  BEGIN

  BEGIN

  RAISE expb;

  EXCEPTION

  WHEN expa THEN

  dbms_output.put_line('yichangAchansheng');

  END;

  EXCEPTION

  WHEN expa THEN

  dbms_output.put_line('yichangAchansheng');

  END;

  ORA-06510: PL/SQL: wufachulideyonghuzidingyiyichangshijian

  ORA-06512: zailine 6

  SQL>

  Bshengmingbufenfashengdeyichang

  shengmingbufenmoufuzhifashengyichanggaiyichangbeilijichuanbodaowaicengyujukuaizhihouanAkezhixingbufenfashengdeyichangguizelaichuli

  SQL> BEGIN

  2 DECLARE

  3 vn NUMBER(1):=25;

  4 BEGIN

  5 NULL;

  6 EXCEPTION

  7 WHEN OTHERS THEN

  8 dbms_output.put_line('yichangzaineicengbeichuli');

  9 END;

  10 EXCEPTION

  11 WHEN OTHERS THEN

  12 dbms_output.put_line('yichangzaiwaicengbeichuli');

  13 END;

  14 /

  yichangzaiwaicengbeichuli

  PL/SQL procedure successfully completed

  SQL>

  Cyichangbufenfashengdeyichang

  yichangchuliqizhongyehuichanshengyichangruRAISEhuoyunxingcuowuchanshengzheliyichanglijibeichuanbodaowaicengtongBshengmingbufenfashengdeyichang

  lizi1

  SQL>

  SQL> BEGIN

  2 DECLARE

  3 expa EXCEPTION;

  4 expb EXCEPTION;

  5 BEGIN

  6 RAISE expa;

  7 EXCEPTION

  8 WHEN expa THEN

  9 RAISE expb;

  10 WHEN expb THEN

  11 --zhelisuiranyouexpbyichangchuliyujudanshigaiyichanghuilijichuanbodaowaiceng

  12 dbms_output.put_line('neicengbuzhuodaoyichangB');

  13 END;

  14 EXCEPTION

  15 WHEN OTHERS THEN

  16 dbms_output.put_line('waicengbuzhuodaoyichangB');

  17 END;

  18 /

  waicengbuzhuodaoyichangB

  PL/SQL procedure successfully completed

  SQL>

  lizi2

  SQL> desc t;

  Name Type

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

  ID NUMBER(1)

  SQL> BEGIN

  2 DECLARE

  3 expa EXCEPTION;

  4 BEGIN

  5 RAISE expa;

  6 EXCEPTION

  7 WHEN expa THEN

  8 INSERT INTO t VALUES(12);

  9 WHEN OTHERS THEN

  10 dbms_output.put_line('neicengbuzhuodaoyichang');

  11 END;

  12 EXCEPTION

  13 WHEN OTHERS THEN

  14 dbms_output.put_line('waicengbuzhuodaoyichang');

  15 END;

  16 /

  waicengbuzhuodaoyichang

  PL/SQL procedure successfully completed

  SQL>

  2007-5-7 19:10:25 yizaiplsqlzhongoraclezidaideExceptionsruxia

  Exception Oracle Error SQLCODE Value

  ACCESS_INTO_NULL ORA-06530 -6530

  CASE_NOT_FOUND ORA-06592 -6592

  COLLECTION_IS_NULL ORA-06531 -6531

  CURSOR_ALREADY_OPEN ORA-06511 -6511

  DUP_VAL_ON_INDEX ORA-00001 -1

  INVALID_CURSOR ORA-01001 -1001

  INVALID_NUMBER ORA-01722 -1722

  LOGIN_DENIED ORA-01017 -1017

  NO_DATA_FOUND ORA-01403 +100

  NOT_LOGGED_ON ORA-01012 -1012

  PROGRAM_ERROR ORA-06501 -6501

  ROWTYPE_MISMATCH ORA-06504 -6504

  SELF_IS_NULL ORA-30625 -30625

  STORAGE_ERROR ORA-06500 -6500

  SUBSCRIPT_BEYOND_COUNT ORA-06533 -6533

  SUBSCRIPT_OUTSIDE_LIMIT ORA-06532 -6532

  SYS_INVALID_ROWID ORA-01410 -1410

  TIMEOUT_ON_RESOURCE ORA-00051 -51

  TOO_MANY_ROWS ORA-01422 -1422

  VALUE_ERROR ORA-06502 -6502

  ZERO_DIVIDE ORA-01476 -1476

  2007-5-7 21:45:42 Oracle SQLCODE/SQLERRM

  OracleneizhihanshuSQLCODEheSQLERRMshitebieyongzaiOTHERSchuliqizhongfenbieyonglaifanhuiOracledecuowudaimahecuowuxiaoxi

  OTHERSchuliqiyinggaishiyichangchulikuaizhongdezuihoudeyichangchuliqiyinweitashiyonglaibuhuochulebiedeyichangchuliqichuliyiwaidesuoyoudeOracleyichangsuoyizaichengxudezuiwaiceng使shiyongyigeOTHERSchuliqidehuajiangkeyiquebaosuoyoudecuowudouhuibeijiancedao

  zaiyigeneizaideyichangzhongSQLCODEfanhuiOraclecuowudexuhaoerSQLERRMfanhuideshixiangyingdecuowuxiaoxicuowuxiaoxishouxianxianshideshicuowudaimaSQLCODEfanhuideshifushuchufeiOracledecuowuweiORA-01403NO DATA FOUNDyiORA-01403weizhaodaoshujudangOraclecuowuweiORA-01403NO DATA FOUNDshiqiduiyingdeSQLCODEwei+100duiyuyonghuzidingyideyichangSQLCODEfanhuideshi+1erSQLERRMfanhuideshiUser-Defined Exception

  yigeOracledecuowuxiaoxizuiduozhinengbaohan512gezijiedecuowudaimaruguomeiyouyichangbeichufazeSQLCODEfanhui0SQLERRMfanhuiORA-0000normal, successful completion原文
 
 
Oracle developer以其快速的数据处理开发而闻名,其异常处理机制也是比较完善,不可小觑。 1、 异常的优点 如果没有异常,在程序中,应当检查每个命令的成功还是失败,如 BEGIN SELECT ... -- check for ’no data found’ error SELECT ... -- check for ’no data found’ error SELECT ... -- check for ’no data found’ error 这种实现的方法缺点在于错误处理没有与正常处理分开,可读性差,使用异常,可以方便处理错误,而且异常处理程序与正常的事务逻辑分开,提高了可读性,如 BEGIN SELECT ... SELECT ... SELECT ... ... EXCEPTION WHEN NO_DATA_FOUND THEN -- catches all ’no data found’ errors 2、 异常的分类 有两种类型的异常,一种为内部异常,一种为用户自定义异常,内部异常是执行期间返回到PL/SQL块的ORACLE错误或由PL/SQL代码的某操作引起的错误,如除数为零或内存溢出的情况。用户自定义异常由开发者显示定义,在PL/SQL块中传递信息以控制对于应用的错误处理。 每当PL/SQL违背了ORACLE原则或超越了系统依赖的原则就会隐式的产生内部异常。因为每个ORACLE错误都有一个号码并且在PL/SQL中异常通过名字处理,ORACLE提供了预定义的内部异常。如SELECT INTO 语句不返回行时产生的ORACLE异常NO_DATA_FOUND。对于预定义异常,现将最常用的异常列举如下: exception oracle error sqlcode value condition no_data_found ora-01403 +100 select into 语句没有符合条件的记录返回 too_mang_rows ora-01422 -1422 select into 语句符合条件的记录有多条返回 dup_val_on_index ora-00001 -1 对于数据库表中的某一列,该列已经被限制为唯一索引,程序试图存储两个重复的值 value_error ora-06502 -6502 在转换字符类型,截取或长度受限时,会发生该异常,如一个字符分配给一个变量,而该变量声明的长度比该字符短,就会引发该异常 storage_error ora-06500 -6500 内存溢出 zero_divide ora-01476 -1476 除数为零 case_not_found ora-06592 -6530 对于选择case语句,没有与之相匹配的条件,同时,也没有else语句捕获其他的条件 cursor_already_open ora-06511 -6511 程序试图打开一个已经打开的游标 timeout_on_resource ora-00051 -51 系统在等待某一资源,时间超时 如果要处理未命名的内部异常,必须使用OTHERS异常处理器或PRAGMA EXCEPTION_INIT 。PRAGMA由编译器控制,或者是对于编译器的注释。PRAGMA在编译时处理,而不是在运行时处理。EXCEPTION_INIT告诉编译器将异常名与ORACLE错误码结合起来,这样可以通过名字引用任意的内部异常,并且可以通过名字为异常编写一适当的异常处理器。 在子程序中使用EXCEPTION_INIT的语法如下: PRAGMA EXCEPTION_INIT(exception_name, -Oracle_error_number); 在该语法中,异常名是声明的异常,下例是其用法: DECLARE deadlock_detected EXCEPTION; PRAGMA EXCEPTION_INIT(deadlock_detected, -60); BEGIN ... -- Some operation that causes an ORA-00060 error EXCEPTION WHEN deadlock_detected THEN -- handle the error END; 对于用户自定义异常,只能在PL/SQL块中的声明部分声明异常,异常的名字由EXCEPTION关键字引入: reserved_loaned Exception 产生异常后,控制传给了子程序的异常部分,将异常转向各自异常控制块,必须在代码中使用如下的结构处理错误: Exception When exception1 then Sequence of statements; When exception2 then Sequence of statements; When others then 3、异常的抛出 由三种方式抛出异常 ◆1. 通过PL/SQL运行时引擎 ◆2. 使用RAISE语句 ◆3. 调用RAISE_APPLICATION_ERROR存储过程 当数据库或PL/SQL在运行时发生错误时,一个异常被PL/SQL运行时引擎自动抛出。异常也可以通过RAISE语句抛出 RAISE exception_name; 显式抛出异常是程序员处理声明的异常的习惯用法,但RAISE不限于声明了的异常,它可以抛出任何任何异常。例如,你希望用TIMEOUT_ON_RESOURCE错误检测新的运行时异常处理器,你只需简单的在程序中使用下面的语句: RAISE TIMEOUT_ON_RESOUCE; 比如下面一个订单输入的例子,若当订单小于库存数量,则抛出异常,并且捕获该异常,处理异常 DECLARE inventory_too_low EXCEPTION; ---其他声明语句 BEGIN IF order_rec.qty>inventory_rec.qty THEN RAISE inventory_too_low; END IF EXCEPTION WHEN inventory_too_low THEN order_rec.staus:='backordered'; END; RAISE_APPLICATION_ERROR内建函数用于抛出一个异常并给异常赋予一个错误号以及错误信息。自定义异常的缺省错误号是+1,缺省信息是User_Defined_Exception。RAISE_APPLICATION_ERROR函数能够在pl/sql程序块的执行部分和异常部分调用,显式抛出带特殊错误号的命名异常。 Raise_application_error(error_number,message[,true,false])) 错误号的范围是-20,000到-20,999。错误信息是文本字符串,最多为2048字节。TRUE和FALSE表示是添加(TRUE)进错误堆(ERROR STACK)还是覆盖(overwrite)错误堆(FALSE)。缺省情况下是FALSE。 如下代码所示: IF product_not_found THEN RAISE_APPLICATION_ERROR(-20123,'Invald product code' TRUE); END IF; 4、异常的处理 PL/SQL程序块的异常部分包含了程序处理错误的代码,当异常被抛出时,一个异常陷阱就自动发生,程序控制离开执行部分转入异常部分,一旦程序进入异常部分就不能再回到同一块的执行部分。下面是异常部分的一般语法: EXCEPTION WHEN exception_name THEN Code for handing exception_name [WHEN another_exception THEN Code for handing another_exception] [WHEN others THEN   code for handing any other exception.] 用户必须在独立的WHEN子串中为每个异常设计异常处理代码,WHEN OTHERS子串必须放置在最后面作为缺省处理器处理没有显式处理的异常。当异常发生时,控制转到异常部分,ORACLE查找当前异常相应的WHEN..THEN语句,捕捉异常,THEN之后的代码被执行,如果错误陷阱代码只是退出相应的嵌套块,那么程序将继续执行内部块END后面的语句。如果没有找到相应的异常陷阱,那么将执行WHEN OTHERS。在异常部分WHEN 子串没有数量限制。 EXCEPTION WHEN inventory_too_low THEN order_rec.staus:='backordered'; replenish_inventory(inventory_nbr=> inventory_rec.sku,min_amount=>order_rec.qty-inventory_rec.qty); WHEN discontinued_item THEN --code for discontinued_item processing WHEN zero_divide THEN --code for zero_divide WHEN OTHERS THEN --code for any other exception END; 当异常抛出后,控制无条件转到异常部分,这就意味着控制不能回到异常发生的位置,当异常被处理和解决后,控制返回到上一层执行部分的下一条语句。 BEGIN DECLARE bad_credit exception; BEGIN RAISE bad_credit; --发生异常,控制转向; EXCEPTION WHEN bad_credit THEN dbms_output.put_line('bad_credit'); END; --bad_credit异常处理后,控制转到这里 EXCEPTION WHEN OTHERS THEN --控制不会从bad_credit异常转到这里 --因为bad_credit已被处理 END; 当异常发生时,在块的内部没有该异常处理器时,控制将转到或传播到上一层块的异常处理部分。 BEGIN DECLARE ---内部块开始 bad_credit exception; BEGIN RAISE bad_credit; --发生异常,控制转向; EXCEPTION WHEN ZERO_DIVIDE THEN --不能处理bad_credite异常 dbms_output.put_line('divide by zero error'); END --结束内部块 --控制不能到达这里,因为异常没有解决; --异常部分 EXCEPTION WHEN OTHERS THEN --由于bad_credit没有解决,控制将转到这里 END; 5、异常的传播 没有处理的异常将沿检测异常调用程序传播到外面,当异常被处理并解决或到达程序最外层传播停止。在声明部分抛出的异常将控制转到上一层的异常部分。 BEGIN executable statements BEGIN today DATE:='SYADATE'; --ERRROR BEGIN --内部块开始 dbms_output.put_line('this line will not execute'); EXCEPTION WHEN OTHERS THEN --异常不会在这里处理 END;--内部块结束 EXCEPTION WHEN OTHERS THEN 处理异常 END 执行部分抛出的异常将首先传递到同一块的异常部分,如果在同一块的异常部分没有处理这个异常的处理器,那么异常将会传播到上一层的异常部分中,一直到最外层。 在异常部分抛出的异常将控制转到上一层的异常部分。 另外错误报告函数SQLCODE和SQLERRM在OTHERS处理器中特别有用,因为它们返回ORACLE错误代码和消息。如下例所示: declare err_num NUMBER; err_msg VARCHAR2(100); BEGIN ... EXCEPTION WHEN OTHERS THEN err_num := SQLCODE; err_msg := SUBSTR(SQLERRM, 1, 100); INSERT INTO errors VALUES (err_num, err_msg); END; 2007-5-7 19:09:02 查看评语»»» 2007-5-7 19:09:43 oracle 异常处理 ~~~~有关于异常处理的3个知识点! 1.EXCEPTION_INT编译指示 功能是将某命名异常同某特定Oracle错误关联起来. 主用用来捕捉某特定异常错误,而不是通过OTHERS来处理. 语法:PROGMA EXCEPTION_INIT(exception_name,oracle_error_number) SQL> SQL> DECLARE 2 expa EXCEPTION; 3 PRAGMA EXCEPTION_INIT(expa,-6502); 4 vn NUMBER(1); 5 BEGIN 6 vn:=24; 7 EXCEPTION 8 WHEN expa THEN 9 dbms_output.put_line('数字或值错误 : 数值精度太高'); 10 WHEN OTHERS THEN 11 dbms_output.put_line(SQLCODE); 12 dbms_output.put_line(substr(SQLERRM,1,100)); 13 END; 14 / 数字或值错误 : 数值精度太高 PL/SQL procedure successfully completed SQL> 2.RAISE_APPLICATION_ERROR 功能用来自定义错误消息 语法: RAISE_APPLICATION_ERROR(error_number,error_message,[keep_errors]) error_number:-20 000到-20 999之间的值 error_message:必须少于512个字符 keep_errors:布尔值.为真则新的错误将被加到已存在的错误清单中(如果已存在的话),为假(默认值)则新的错误将代替当前的错误清单. SQL> BEGIN 2 UPDATE t SET a='TEST' WHERE a='12345'; 3 IF SQL%NOTFOUND THEN 4 raise_application_error(-20001,'你傻了?知道没有这样的数据还去更新!'); 5 END IF; 6 EXCEPTION 7 WHEN OTHERS THEN 8 dbms_output.put_line(substr(SQLERRM,1,100)); 9 END; 10 / ORA-20001: 你傻了?知道没有这样的数据还去更新! PL/SQL procedure successfully completed SQL> 3.异常传播 A可执行部分发生的异常 1)如果当前语句块有该异常的处理程序则执行之,控制权交由外层语句块. 当前语句块有该异常的处理程序: SQL> DECLARE 2 expa EXCEPTION; 3 expb EXCEPTION; 4 BEGIN 5 BEGIN 6 RAISE expa; 7 EXCEPTION 8 WHEN expa THEN 9 dbms_output.put_line('异常A产生'); 10 END; 11 EXCEPTION 12 WHEN expb THEN 13 dbms_output.put_line('异常B产生'); 14 END; 15 / 异常A产生 PL/SQL procedure successfully completed SQL> 2)如果当前语句没有该异常的处理程序则通过外层语句块中产生该异常来传播该异常,然后通过外层异常处理程序按步骤1来处理.若外层没有该异常的处理程序则异常传播到调用环境. 当前语句块没有该异常的处理程序,外层语句块中产生该异常并处理 SQL> DECLARE 2 expa EXCEPTION; 3 expb EXCEPTION; 4 BEGIN 5 BEGIN 6 RAISE expb; 7 EXCEPTION 8 WHEN expa THEN 9 dbms_output.put_line('异常A产生'); 10 END; 11 EXCEPTION 12 WHEN expb THEN 13 dbms_output.put_line('异常B产生'); 14 END; 15 / 异常B产生 PL/SQL procedure successfully completed SQL> 当前语句块没有该异常的处理程序,外层语句块中产生该异常且没有该异常处理程序,异常传播到调用环境 SQL> DECLARE 2 expa EXCEPTION; 3 expb EXCEPTION; 4 BEGIN 5 BEGIN 6 RAISE expb; 7 EXCEPTION 8 WHEN expa THEN 9 dbms_output.put_line('异常A产生'); 10 END; 11 EXCEPTION 12 WHEN expa THEN 13 dbms_output.put_line('异常A产生'); 14 END; 15 / DECLARE expa EXCEPTION; expb EXCEPTION; BEGIN BEGIN RAISE expb; EXCEPTION WHEN expa THEN dbms_output.put_line('异常A产生'); END; EXCEPTION WHEN expa THEN dbms_output.put_line('异常A产生'); END; ORA-06510: PL/SQL: 无法处理的用户自定义异常事件 ORA-06512: 在line 6 SQL> B声明部分发生的异常 声明部分某赋值发生异常,该异常被立即传播到外层语句块,之后按"A可执行部分发生的异常"规则来处理 SQL> BEGIN 2 DECLARE 3 vn NUMBER(1):=25; 4 BEGIN 5 NULL; 6 EXCEPTION 7 WHEN OTHERS THEN 8 dbms_output.put_line('异常在内层被处理!'); 9 END; 10 EXCEPTION 11 WHEN OTHERS THEN 12 dbms_output.put_line('异常在外层被处理!'); 13 END; 14 / 异常在外层被处理! PL/SQL procedure successfully completed SQL> C异常部分发生的异常 异常处理器中也会产生异常,如RAISE或运行错误产生,这里异常立即被传播到外层同"B声明部分发生的异常" 例子1 SQL> SQL> BEGIN 2 DECLARE 3 expa EXCEPTION; 4 expb EXCEPTION; 5 BEGIN 6 RAISE expa; 7 EXCEPTION 8 WHEN expa THEN 9 RAISE expb; 10 WHEN expb THEN 11 --这里虽然有expb异常处理语句,但是该异常会立即传播到外层 12 dbms_output.put_line('内层捕捉到异常B'); 13 END; 14 EXCEPTION 15 WHEN OTHERS THEN 16 dbms_output.put_line('外层捕捉到异常B'); 17 END; 18 / 外层捕捉到异常B PL/SQL procedure successfully completed SQL> 例子2 SQL> desc t; Name Type ---- --------- ID NUMBER(1) SQL> BEGIN 2 DECLARE 3 expa EXCEPTION; 4 BEGIN 5 RAISE expa; 6 EXCEPTION 7 WHEN expa THEN 8 INSERT INTO t VALUES(12); 9 WHEN OTHERS THEN 10 dbms_output.put_line('内层捕捉到异常'); 11 END; 12 EXCEPTION 13 WHEN OTHERS THEN 14 dbms_output.put_line('外层捕捉到异常'); 15 END; 16 / 外层捕捉到异常 PL/SQL procedure successfully completed SQL> 2007-5-7 19:10:25 一、在plsql中,oracle自带的Exceptions如下: Exception Oracle Error SQLCODE Value ACCESS_INTO_NULL ORA-06530 -6530 CASE_NOT_FOUND ORA-06592 -6592 COLLECTION_IS_NULL ORA-06531 -6531 CURSOR_ALREADY_OPEN ORA-06511 -6511 DUP_VAL_ON_INDEX ORA-00001 -1 INVALID_CURSOR ORA-01001 -1001 INVALID_NUMBER ORA-01722 -1722 LOGIN_DENIED ORA-01017 -1017 NO_DATA_FOUND ORA-01403 +100 NOT_LOGGED_ON ORA-01012 -1012 PROGRAM_ERROR ORA-06501 -6501 ROWTYPE_MISMATCH ORA-06504 -6504 SELF_IS_NULL ORA-30625 -30625 STORAGE_ERROR ORA-06500 -6500 SUBSCRIPT_BEYOND_COUNT ORA-06533 -6533 SUBSCRIPT_OUTSIDE_LIMIT ORA-06532 -6532 SYS_INVALID_ROWID ORA-01410 -1410 TIMEOUT_ON_RESOURCE ORA-00051 -51 TOO_MANY_ROWS ORA-01422 -1422 VALUE_ERROR ORA-06502 -6502 ZERO_DIVIDE ORA-01476 -1476 2007-5-7 21:45:42 Oracle SQLCODE/SQLERRM Oracle内置函数SQLCODE和SQLERRM是特别用在OTHERS处理器中,分别用来返回Oracle的错误代码和错误消息。 OTHERS处理器应该是异常处理块中的最后的异常处理器,因为它是用来捕获除了别的异常处理器处理以外的所有的Oracle异常,所以在程序的最外层使用一个OTHERS处理器的话,将可以确保所有的错误都会被检测到。 在一个内在的异常中,SQLCODE返回Oracle错误的序号,而SQLERRM返回的是相应的错误消息,错误消息首先显示的是错误代码。SQLCODE返回的是负数,除非Oracle的错误为“ORA-01403:NO DATA FOUND”(译:ORA-01403:未找到数据),当Oracle错误为“ORA-01403:NO DATA FOUND”时,其对应的SQLCODE为+100。对于用户自定义的异常,SQLCODE返回的是+1,而SQLERRM返回的是User-Defined Exception。 一个Oracle的错误消息最多只能包含512个字节的错误代码。如果没有异常被触发,则SQLCODE返回0,SQLERRM返回“ORA-0000:normal, successful completion”。
󰈣󰈤
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有