Oracle PL/SQL之EXCEPTION

來源:互聯網
上載者:User

Test Code:

  1. DECLARE  
  2. BEGIN  
  3.   
  4.   <<test0>>  
  5. -- most normal way to handle exception.  
  6.   DECLARE  
  7.     except_test0 EXCEPTION;  
  8.   BEGIN  
  9.     RAISE except_test0;  
  10.   EXCEPTION  
  11.     WHEN except_test0 THEN  
  12.       dbms_output.put_line('test0 except_test0: SQLCODE=' || SQLCODE ||  
  13.                            ', SQLERRM=' || SQLERRM);  
  14.     WHEN OTHERS THEN  
  15.       dbms_output.put_line('test0 OTHERS: SQLCODE=' || SQLCODE ||  
  16.                            ', SQLERRM=' || SQLERRM);  
  17.   END;  
  18.   
  19.   <<test1>>  
  20. -- custom exception error number.  
  21.   DECLARE  
  22.     except_test1 EXCEPTION;  
  23.     -- suggested error number range: -20,NNN.  
  24.     PRAGMA EXCEPTION_INIT(except_test1, -20001);  
  25.   BEGIN  
  26.     RAISE except_test1;  
  27.   EXCEPTION  
  28.     WHEN OTHERS THEN  
  29.       dbms_output.put_line('test1: SQLCODE=' || SQLCODE || ', SQLERRM=' ||  
  30.                            SQLERRM);  
  31.   END;  
  32.   
  33.   <<test2>>  
  34. -- custom exception error number and error message.  
  35.   BEGIN  
  36.     raise_application_error(-20002, 'except test 2');  
  37.   EXCEPTION  
  38.     WHEN OTHERS THEN  
  39.       IF SQLCODE = -20002  
  40.       THEN  
  41.         dbms_output.put_line('test2A: SQLCODE=' || SQLCODE || ', SQLERRM=' ||  
  42.                              SQLERRM);  
  43.         dbms_output.put_line('test2B: SQLCODE=' || SQLCODE || ', SQLERRM=' ||  
  44.                              SQLERRM);  
  45.       ELSE  
  46.         dbms_output.put_line('test2C: SQLCODE=' || SQLCODE || ', SQLERRM=' ||  
  47.                              SQLERRM);  
  48.       END IF;  
  49.   END;  
  50.   
  51.   -- SQLCODE and SQLERRM will be re evaluated after EXCEPTION handled.  
  52.   dbms_output.put_line('test2D: SQLCODE=' || SQLCODE || ', SQLERRM=' ||  
  53.                        SQLERRM);  
  54.   
  55.   <<test3>>  
  56. -- custom exception error number and error message, more readable.  
  57.   DECLARE  
  58.     except_test3 EXCEPTION;  
  59.     PRAGMA EXCEPTION_INIT(except_test3, -20001);  
  60.   BEGIN  
  61.     raise_application_error(-20001, 'except test 3');  
  62.   EXCEPTION  
  63.     WHEN except_test3 THEN  
  64.       dbms_output.put_line('test3 except_test3: SQLCODE=' || SQLCODE ||  
  65.                            ', SQLERRM=' || SQLERRM);  
  66.     WHEN OTHERS THEN  
  67.       dbms_output.put_line('test3 OTHERS: SQLCODE=' || SQLCODE ||  
  68.                            ', SQLERRM=' || SQLERRM);  
  69.   END;  
  70.   
  71.   <<test4>>  
  72. -- exception can be re raised.  
  73.   BEGIN  
  74.     RAISE no_data_found;  
  75.   EXCEPTION  
  76.     WHEN OTHERS THEN  
  77.       dbms_output.put_line('test4: SQLCODE=' || SQLCODE || ', SQLERRM=' ||  
  78.                            SQLERRM);  
  79.       RAISE;  
  80.   END;  
  81.   
  82. EXCEPTION  
  83.   WHEN OTHERS THEN  
  84.     dbms_output.put_line('outer: SQLCODE=' || SQLCODE || ', SQLERRM=' ||  
  85.                          SQLERRM);  
  86. END;  

Output:

  1. test0 except_test0: SQLCODE=1, SQLERRM=User-Defined Exception  
  2. test1: SQLCODE=-20001, SQLERRM=ORA-20001:   
  3. test2A: SQLCODE=-20002, SQLERRM=ORA-20002: except test 2  
  4. test2B: SQLCODE=-20002, SQLERRM=ORA-20002: except test 2  
  5. test2D: SQLCODE=0, SQLERRM=ORA-0000: normal, successful completion  
  6. test3 except_test3: SQLCODE=-20001, SQLERRM=ORA-20001: except test 3  
  7. test4: SQLCODE=100, SQLERRM=ORA-01403: no data found  
  8. outer: SQLCODE=100, SQLERRM=ORA-01403: no data found  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.