Oracle PL/SQL EXCEPTION

Source: Internet
Author: User

Test Code:

  1. DECLARE
  2. BEGIN
  3. <Test0>
  4. -- Most normal way to handle exception.
  5. DECLARE
  6. Except_test0 EXCEPTION;
  7. BEGIN
  8. RAISE effect_test0;
  9. EXCEPTION
  10. WHEN effect_test0 THEN
  11. Dbms_output.put_line ('Test0 effect_test0: SQLCODE ='| SQLCODE |
  12. ', SQLERRM ='| SQLERRM );
  13. WHEN OTHERS THEN
  14. Dbms_output.put_line ('Test0 OTHERS: SQLCODE ='| SQLCODE |
  15. ', SQLERRM ='| SQLERRM );
  16. END;
  17. <Test1>
  18. -- Custom exception error number.
  19. DECLARE
  20. Except_test1 EXCEPTION;
  21. -- Suggested error number range:-20, NNN.
  22. PRAGMA EXCEPTION_INIT (effect_test1,-20001 );
  23. BEGIN
  24. RAISE effect_test1;
  25. EXCEPTION
  26. WHEN OTHERS THEN
  27. Dbms_output.put_line ('Test1: SQLCODE ='| SQLCODE |', SQLERRM ='|
  28. SQLERRM );
  29. END;
  30. <Test2>
  31. -- Custom exception error number and error message.
  32. BEGIN
  33. Raise_application_error (-20002,'Couldn t test 2');
  34. EXCEPTION
  35. WHEN OTHERS THEN
  36. If SQL code =-20002
  37. THEN
  38. Dbms_output.put_line ('Test2a: SQLCODE ='| SQLCODE |', SQLERRM ='|
  39. SQLERRM );
  40. Dbms_output.put_line ('Test2b: SQLCODE ='| SQLCODE |', SQLERRM ='|
  41. SQLERRM );
  42. ELSE
  43. Dbms_output.put_line ('Test2c: SQLCODE ='| SQLCODE |', SQLERRM ='|
  44. SQLERRM );
  45. End if;
  46. END;
  47. -- SQLCODE and SQLERRM will be re evaluated after EXCEPTION handled.
  48. Dbms_output.put_line ('Test2d: SQLCODE ='| SQLCODE |', SQLERRM ='|
  49. SQLERRM );
  50. <Test3>
  51. -- Custom exception error number and error message, more readable.
  52. DECLARE
  53. Except_test3 EXCEPTION;
  54. PRAGMA EXCEPTION_INIT (effect_test3,-20001 );
  55. BEGIN
  56. Raise_application_error (-20001,'Couldn t test 3');
  57. EXCEPTION
  58. WHEN effect_test3 THEN
  59. Dbms_output.put_line ('Test3 effect_test3: SQLCODE ='| SQLCODE |
  60. ', SQLERRM ='| SQLERRM );
  61. WHEN OTHERS THEN
  62. Dbms_output.put_line ('Test3 OTHERS: SQLCODE ='| SQLCODE |
  63. ', SQLERRM ='| SQLERRM );
  64. END;
  65. <Test4>
  66. -- Exception can be re raised.
  67. BEGIN
  68. RAISE no_data_found;
  69. EXCEPTION
  70. WHEN OTHERS THEN
  71. Dbms_output.put_line ('Test4: SQLCODE ='| SQLCODE |', SQLERRM ='|
  72. SQLERRM );
  73. RAISE;
  74. END;
  75. EXCEPTION
  76. WHEN OTHERS THEN
  77. Dbms_output.put_line ('Outer: SQLCODE ='| SQLCODE |', SQLERRM ='|
  78. SQLERRM );
  79. END;

Output:

  1. Test0 effect_test0: SQLCODE = 1, SQLERRM = User-Defined Exception
  2. Test1: SQLCODE =-20001, SQLERRM = ORA-20001:
  3. Test2A: SQLCODE =-20002, SQLERRM = ORA-20002: Counter T test 2
  4. Test2B: SQLCODE =-20002, SQLERRM = ORA-20002: Counter T test 2
  5. Test2D: SQLCODE = 0, SQLERRM = ORA-0000: normal, successful completion
  6. Test3 effect_test3: SQLCODE =-20001, SQLERRM = ORA-20001: couldn t test 3
  7. Test4: SQLCODE = 100, SQLERRM = ORA-01403: no data found
  8. Outer: SQLCODE = 100, SQLERRM = ORA-01403: no data found

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.