Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For a start catching <em>everything</em> using <code>EXCEPTION WHEN OTHERS</code> is not necessarily the best practice. If you handle an exception you should know exactly what you're going to do with it. It's unlikely that you have the ability to handle every single Oracle exception properly using OTHERS and if you do so you should probably be logging them somewhere where they'll be noticed.</p> <p>To quote from Oracle's <a href="http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/errors.htm#BCFEBJBF" rel="nofollow">Guidelines for Avoiding and Handling Exceptions</a>:</p> <blockquote> <ul> <li><p>Whenever possible, write exception handlers for named exceptions instead of using OTHERS exception handlers.</p> <p>Learn the names and causes of the predefined exceptions. If you know that your database operations might raise specific internally defined exceptions that do not have names, then give them names so that you can write exception handlers specifically for them.</p></li> </ul> </blockquote> <p>To answer your question you can use the <a href="http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/errors.htm#LNPLS99960" rel="nofollow"><code>raise_application_error</code></a> procedure to raise an error at any point in your code:</p> <pre><code>RAISE_APPLICATION_ERROR (-20000, 'an exception'); </code></pre> <p>The user defined exceptions are those between 20,000 and 20,999 so ensure you use a number in this range.</p> <p>An example could be:</p> <pre><code>begin raise_application_error(-20500, 'an exception occurred'); exception when others then do_something; end; / </code></pre> <p>You can also define your own <a href="http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/exception_declaration.htm#CJABADFA" rel="nofollow">pre-defined exception</a>, which you can then catch:</p> <pre><code>declare my_exception EXCEPTION; PRAGMA EXCEPTION_INIT( my_exception, -20001 ); begin raise_application_error( -20001, 'an exception' ); exception when my_exception then do_something; end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload