WELCOME, GUEST
Minimize
Blogger List

Steven Feuerstein Indicates Oracle ACE director status
PL/SQL Obsession

Guy Harrison Indicates Oracle ACE status
Database topics

Bert Scalzo Indicates Oracle ACE status
Toad for Oracle, Data Modeling, Benchmarking
Dan Hotka Indicates Oracle ACE director status
SQL Tuning & PL/SQL Tips

Valentin Baev
It's all about Toad

Ben Boise
Toad SC Discussions

Dan Clamage
SQL and PL/SQL

Kevin Dalton
Benchmark Factory

Peter Evans 
Business Intelligence, Data Integration, Cloud and Big Data

Vaclav Frolik  
Toad Data Modeler, Toad Extension for Eclipse

Devin Gallagher
Toad SC discussions

Anju Gandhi
Toad for Oracle

Stuart Hodgins
JProbe Discussions

Julie Hyman
Toad for Data Analysts

  Henrik "Mauritz" Johnson
Toad Tips & Tricks on the "other" Toads
  Mark Kurtz
Toad SC discussions
Daniel Norwood
Tips & Tricks on Toad Solutions
Amit Parikh
Toad for Oracle, Benchmark Factory,Quest Backup Reporter
Debbie Peabody
Toad Data Point
Gary Piper
Toad Reports Manager
John Pocknell
Toad Solutions
Jeff Podlasek
Toad for DB2
Kuljit Sangha
Toad SC discussions
Michael Sass 
Toad for DB2
Brad Wulf
Toad SC discussions
Richard To
SQL Optimization
  Toad Data Modeler Opens in a new window
Data Modeling
 
  Toad Higher Education
How Hi-Ed Uses Toad
  Real Automated Code Testing for Oracle
Quest Code Tester blog
  中文技术资料库
技术文章
 

Blogs

Toad World blogs are a mix of insightful how-tos from Quest experts as well as their commentary on experiences with new database technologies.  Have some views of your own to share?  Post your comments!  Note:  Comments are restricted to registered Toad World users.

Do you have a topic that you'd like discussed?  We'd love to hear from you.  Send us your idea for a blog topic.


Jul 14

Written by: StevenFeuersteinTW
Wednesday, July 14, 2010 1:02 PM  RssIcon

A PL/SQL developer just sent me the following question:
"I can't understand the difference between RAISE_APPLICATION_ERROR and RAISE. Why would I use one vs. the other?"
Instead of answering via an email to just one developer, I thought I might share my answer with everyone.
 
The RAISE_APPLICATION_ERROR built-in (defined in the DBMS_STANDARD package) should be used for just a single scenario: you need to communicate an application-specific error back to the user.
 
Suppose, for example, I have a rule for the employees table that the minimum salary allowed is $100,000 (ah, wouldn't that be nice?). I want to enforce that rule through a database trigger:
TRIGGER employees_minsal_tr
   BEFORE INSERT OR UPDATE
   ON employees
   FOR EACH ROW
BEGIN
   IF :new.salary < 100000
   THEN
      /* communicate error */
      NULL;
   END IF;
END;

I can stop the DML from completing by issuing a RAISE statement, such as:

RAISE PROGRAM_ERROR;
But I would not be able to communicate back to the user what the actual problem was.
 
If, on the other hand, I use RAISE_APPLICATION_ERROR, I can specify the error number (of little interest to my users, but a good "identifier" for support) and, more importantly, the error message, as in:
TRIGGER employees_minsal_tr
   BEFORE INSERT OR UPDATE
   ON employees
   FOR EACH ROW
BEGIN
   IF :new.salary < 1000000
   THEN
      RAISE_APPLICATION_ERROR (-20000,
          'Salary of '|| :new.salary ||
          ' is too low. It must be at least $100,000.');
   END IF;
END; 
And that, dear reader, is the motivation for using RAISE_APPLICATION_ERROR: the ability to communicate a custom, application-specific error message to your users.
 
Use RAISE when you want to raise an already-defined exception, whether one of Oracle's (such as NO_DATA_FOUND) or one of your definition, as in:
DECLARE
   e_bad_value EXCEPTION;
BEGIN
   RAISE e_bad_value;
END; 
but if it is one of your own user-defined exceptions, it only makes sense to raise it this way if you are going to trap it inside the backend as well, and then do something in response to the error.
 
If you let the e_bad_value exception propagate out to your users, all they will know about the error is that the error code is 1 and the error message is "User-defined Exception". Not very helpful.
 
 

5 comment(s) so far...


Re: RAISE vs RAISE_APPLICATION_ERROR?

Thank you very much Sir.

By satya82 on   Thursday, July 15, 2010 8:24 AM

Re: RAISE vs RAISE_APPLICATION_ERROR?

The IF test has too many zeroes: it's actually checking for a minimum salary of $1,000,000! Wouldn't THAT be nice!

By AndyDan on   Monday, August 02, 2010 12:37 PM
Gravatar

Re: RAISE vs RAISE_APPLICATION_ERROR?

Very helpful. Just noticed a typo error on the second script on the blog - it says "RAISE_APPLICATIONE_ERROR".

By DamerBullet on   Thursday, September 23, 2010 7:18 AM
Gravatar

Re: RAISE vs RAISE_APPLICATION_ERROR?

I got the concept between raise and raise_application_error but just for your last point

"If you let the e_bad_value exception propagate out to your users, all they will know about the error is that the error code is 1 and the error message is "User-defined Exception". Not very helpful."

If I use dbms_output.put_line to display the custom error message from exception handler then will this make any sence, knowing that I will be using the custom exception only for a specific purpose?
Is there anything extra we are getting with raise_application_error instead using raise with dbms_output.put_line?

By VISHALAKSHA on   Wednesday, December 01, 2010 2:01 PM
Gravatar

Re: RAISE vs RAISE_APPLICATION_ERROR?

Generally, I would not rely on PUT_LINE to communicate information back to a user. If this is "just" an anonymous block/script you are running, then I suppose there isn't a big difference. But RAE has the advantage of setting the error code and error message, so a call to SQLCODE and SQLERRM or DBMS_UTILITY.FORMAT_ERROR_STACK returns YOUR values, which can then be accessed and presented to the user in a more structured fashion than output to a screen.

By StevenFeuersteinTW on   Tuesday, December 07, 2010 8:59 AM
Search Blog Entries
 
Blog Archives
 
Archive
<May 2013>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
Monthly
May, 2013 (15)
April, 2013 (13)
March, 2013 (10)
February, 2013 (5)
January, 2013 (7)
December, 2012 (6)
November, 2012 (10)
October, 2012 (8)
September, 2012 (6)
August, 2012 (8)
July, 2012 (8)
June, 2012 (12)
May, 2012 (21)
April, 2012 (10)
March, 2012 (16)
February, 2012 (19)
January, 2012 (20)
December, 2011 (19)
November, 2011 (14)
October, 2011 (12)
September, 2011 (17)
August, 2011 (15)
July, 2011 (16)
June, 2011 (13)
May, 2011 (15)
April, 2011 (8)
March, 2011 (21)
February, 2011 (17)
January, 2011 (16)
December, 2010 (13)
November, 2010 (13)
October, 2010 (7)
September, 2010 (15)
August, 2010 (11)
July, 2010 (13)
June, 2010 (12)
May, 2010 (14)
April, 2010 (12)
March, 2010 (13)
February, 2010 (12)
January, 2010 (7)
December, 2009 (10)
November, 2009 (12)
October, 2009 (15)
September, 2009 (18)
August, 2009 (13)
July, 2009 (23)
June, 2009 (14)
May, 2009 (17)
April, 2009 (7)
March, 2009 (14)
February, 2009 (7)
January, 2009 (12)
December, 2008 (7)
November, 2008 (11)
October, 2008 (19)
September, 2008 (14)
August, 2008 (11)
July, 2008 (14)
June, 2008 (19)
May, 2008 (12)
April, 2008 (18)
March, 2008 (13)
February, 2008 (8)
January, 2008 (7)
December, 2007 (5)
November, 2007 (8)
October, 2007 (13)
September, 2007 (13)
August, 2007 (16)
July, 2007 (11)
June, 2007 (6)
May, 2007 (5)
April, 2007 (5)
March, 2007 (8)
February, 2007 (6)
January, 2007 (6)
December, 2006 (5)
November, 2006 (8)
October, 2006 (4)
August, 2006 (3)