Oracle PL/SQL exception

Source: Internet
Author: User

Test code:

Declare <br/> begin </P> <p> <test0> <br/> -- most normal way to handle exception. <br/> declare <br/> except_test0 exception; <br/> begin <br/> raise effect_test0; <br/> exception <br/> when effect_test0 then <br/> dbms_output.put_line ('test0 effect_test0: sqlcode = '| sqlcode ||< br/> ', sqlerrm = '| sqlerrm); <br/> when others then <br/> dbms_output.put_line ('test0 others: sqlcode =' | sqlcode | <br/> ', sqlerrm = '| sqlerrm); <br/> end; </P> <p> <test1> <br/> -- custom Exception error number. <br/> declare <br/> except_test1 exception; <br/> -- suggested error number range:-20, NNN. <br/> Pragma exception_init (effect_test1,-20001); <br/> begin <br/> raise effect_test1; <br/> exception <br/> when others then <br/> dbms_output.put_line ('test1: sqlcode = '| sqlcode | ', sqlerrm = '| <br/> sqlerrm); <br/> end; </P> <p> <Test2> <br/> -- custom Exception error number and error message. <br/> begin <br/> raise_application_error (-20002, 'doesn t test 2 '); <br/> exception <br/> when others then <br/> If sqlcode =-20002 <br/> then <br/> dbms_output.put_line ('test2a: sqlcode = '| sqlcode |', sqlerrm = '| <br/> sqlerrm); <br/> dbms_output.put_line ('test2b: sqlcode = '| sqlcode |', sqlerrm = '| <br/> sqlerrm); <br/> else <br/> dbms_output.put_line ('test2c: sqlcode = '| sqlcode |', sqlerrm = '| <br/> sqlerrm); <br/> end if; <br/> end; </P> <p> -- sqlcode and sqlerrm will be re evaluated after exception handled. <br/> dbms_output.put_line ('test2d: sqlcode = '| sqlcode |', sqlerrm = '| <br/> sqlerrm ); </P> <p> <test3> <br/> -- custom Exception error number and error message, more readable. <br/> declare <br/> except_test3 exception; <br/> Pragma exception_init (effect_test3,-20001); <br/> begin <br/> raise_application_error (-20001, 'couldn t test 3'); <br/> exception <br/> when effect_test3 then <br/> dbms_output.put_line ('test3 effect_test3: sqlcode = '| sqlcode | <br/>', sqlerrm = '| sqlerrm); <br/> when others then <br/> dbms_output.put_line ('test3 others: sqlcode = '| sqlcode | <br/>', sqlerrm = '| sqlerrm); <br/> end; </P> <p> <test4> <br/> -- exception can be re raised. <br/> begin <br/> raise no_data_found; <br/> exception <br/> when others then <br/> dbms_output.put_line ('test4: sqlcode = '| sqlcode |', sqlerrm = '| <br/> sqlerrm); <br/> raise; <br/> end; </P> <p> exception <br/> when others then <br/> dbms_output.put_line ('outer: sqlcode = '| sqlcode | ', sqlerrm = '| <br/> sqlerrm); <br/> end; <br/>

 

Output:

Test0 effect_test0: sqlcode = 1, sqlerrm = user-defined exception <br/> test1: sqlcode =-20001, sqlerrm = ORA-20001: <br/> test2a: sqlcode =-20002, sqlerrm = ORA-20002: couldn t test 2 <br/> test2b: sqlcode =-20002, sqlerrm = ORA-20002: couldn t test 2 <br/> test2d: sqlcode = 0, sqlerrm = ORA-0000: normal, successful completion <br/> test3 effect_test3: sqlcode =-20001, sqlerrm = ORA-20001: using t test 3 <br/> test4: sqlcode = 100, sqlerrm = ORA-01403: no data found <br/> outer: sqlcode = 100, sqlerrm = ORA-01403: No data found <br/>

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.