小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

oracle異常處理

 路人甲Java 2022-03-15

語句執(zhí)行過程中,由于各種原因使得語句不能正常執(zhí)行,可能會造成更大錯誤或整個系統(tǒng)的崩潰,所以PS/SQL提供了異常(exception)著一處理的方法來防止此類情況的發(fā)生。在代碼運(yùn)行的過程中無論何時發(fā)生錯誤,PL/SQL都能控制程序自動地轉(zhuǎn)向執(zhí)行異常部分。

1.預(yù)定義異常

預(yù)定義異常是由于系統(tǒng)產(chǎn)生的。例如出現(xiàn)0除,PL/SQL就會產(chǎn)生一個預(yù)定義的ZERO_DIVIDE異常。

--ZERO_DIVIDE異常。使用系統(tǒng)預(yù)定義的異常處理,使用該處理后,程序運(yùn)行時系統(tǒng)就不會提示出現(xiàn)錯誤。
 
declare
  v_number1 number(3):=10;
  v_number2 number(3):=0;
  v_number3 number(3);
  
  begin
     v_number3:=v_number1/v_number2;
     DBMS_OUTPUT.PUT_LINE(v_number3);
     EXCEPTION
        when ZERO_DIVIDE then
          DBMS_OUTPUT.PUT_LINE('除數(shù)不能為0');
  end;
輸出結(jié)果:DIVIDE ZERO

 

2.PL/SQL中常見的異常:

 

 

3.轉(zhuǎn)換的錯誤處理

declare
  v_number1 number(3);
  v_char  char(5):='123c';
  begin
     v_number1:=to_number(v_char);  //將字符轉(zhuǎn)換成數(shù)字
     DBMS_OUTPUT.PUT_LINE('轉(zhuǎn)換成功');
     EXCEPTION
        when value_error then
          DBMS_OUTPUT.PUT_LINE('轉(zhuǎn)換沒成功');
  end;

 

4.聯(lián)合的錯誤處理

declare
   v_name  school_students.stu_name%type;
   begin
      select stu_name into v_name
      from school_students
      where stu_id='2016322180021';
      dbms_output.put_line(v_name);
      EXCEPTION
        when  no_data_found then
           dbms_output.put_line('沒有數(shù)據(jù)');
        when  too_many_rows then
           dbms_output.put_line('數(shù)據(jù)太多');
        when  ZERO_DIVIDE then
           dbms_output.put_line('列出錯列');
      
  end;

 

5.用戶定義異常

--用戶可以通過自定義異常來處理發(fā)生的錯誤,語法格式為:
exception
   when 異常名 then
        語句塊 1;
    when  then
        語句塊2;
    [when others then
         語句塊3;]
end;

 注意:每個異常處理部分都是由when子句和相應(yīng)的執(zhí)行語句組成

 

6.自定義異常

declare
   e_name  exception;
   v_num  number(8);
   begin
      select count(*) into v_num
      from school_students;
     if v_num>10 then
        RAISE  e_name;
     end if ;
     exception
       when e_name then
       dbms_output.put_line('最大值不能超過10');
    end;

注意:同一個異常不允許多個when子句來處理,一個異常對應(yīng)一個when子句。

 

7.使用others異常

declare
   v_name  school_students.stu_name%type;
   begin
      select stu_name into v_name
      from school_students
      where stu_id='2016322180021';
      dbms_output.put_line(v_name);
      EXCEPTION
        when  OTHERS then
           dbms_output.put_line('出錯了');
  end;

對于一個異常有兩個處理方式,分別位于不同的when子句,因此系統(tǒng)會認(rèn)為是不合法的。可以使用others來處理那些不能由其他when子句處理的異常,others異常處理總是位于exception語句的最后。

其實,others異常處理可以借助兩個函數(shù)來說明捕捉到的異常的類型,這兩個函數(shù)是PL/SQL和SQLERRM,其中SQLLOCODE是用來說明當(dāng)前錯誤的代碼,如果是用戶自定義異常。則返回1.SQLERRM返回的是當(dāng)前錯誤的信息。

 

8.空操作和空值

declare
  n number(3):=-1;
  begin
     if n<0 then
         null;
     else
        DBMS_OUTPUT.PUT_LINE('正常');
     end if;
  end;

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多