Using raise_application_error - user-defined exceptions, PL-SQL Programming

Assignment Help:

Using raise_application_error

The Package DBMS_STANDARD that is supplied with Oracle gives language facilities that help your application to interact with Oracle. For illustration, the procedure raise_application_error lets you issue user-defined error messages from the stored subprograms. In that way, you can report errors to your application and avoid returning unhandled exceptions. To call the raise_application_error, use the syntax shown below:

raise_application_error(error_number, message[, {TRUE | FALSE}]);

Where error_number is a negative integer in the range of -20000... -20999 and message is a character string of upto 2048 bytes long. When the optional third parameter is TRUE, then the error is placed on the stack of earlier errors. And If the parameter is FALSE (the default), the error replaces all earlier errors. The Package DBMS_ STANDARD is an extension of the package STANDARD, so you do not require qualifying the references to its contents.

An application can only call the raise_application_error from an executing stored subprogram (or method). Whenever called, raise_application_error ends the subprogram and returns a user-defined error number and message to the application. Error number and the message can be trapped such as any Oracle error.

In the example below, you call the raise_application_error if an employee's salary is missing:

CREATE PROCEDURE raise_salary (emp_id NUMBER, amount NUMBER) AS

curr_sal NUMBER;

BEGIN

SELECT sal INTO curr_sal FROM emp WHERE empno = emp_id;

IF curr_sal IS NULL THEN

/* Issue user-defined error message. */

raise_application_error(-20101, 'Salary is missing');

ELSE

UPDATE emp SET sal = curr_sal + amount WHERE empno = emp_id;

END IF;

END raise_salary;

The calling applications get a PL/SQL exception that can process using the error-reporting functions SQLCODE and SQLERRM in an OTHERS handler. It can also use the pragma EXCEPTION_INIT to map precise error numbers returned by the raise_application_error to exceptions of its own, as shown below:

EXEC SQL EXECUTE

/* Execute embedded PL/SQL block using host

variables my_emp_id and my_amount, which were

assigned values in the host environment. */

DECLARE

...

null_salary EXCEPTION;

/* Map error number returned by raise_application_error

to user-defined exception. */

PRAGMA EXCEPTION_INIT(null_salary, -20101);

BEGIN

...

raise_salary(:my_emp_id, :my_amount);

EXCEPTION

WHEN null_salary THEN

INSERT INTO emp_audit VALUES (:my_emp_id, ...);

...

END;

END-EXEC;

This method allows the calling application to handle error conditions in the specific exception handlers.


Related Discussions:- Using raise_application_error - user-defined exceptions

Using operator value in pl sql, Using Operator VALUE: As you may expec...

Using Operator VALUE: As you may expect, the operator VALUE returns the value of an object. The VALUE takes its argument a correlation variable.  For illustration, to return a

Triffic light control system, write the program for traffic control system ...

write the program for traffic control system with 10 second, 15 secod, and 20 second delay

Data type conversion, Datatype Conversion At times it is necessary to c...

Datatype Conversion At times it is necessary to convert a value from one datatype to another. For e.g. if you want to inspect a rowid, you should convert it to a character stri

Use of count in sql, Use of COUNT in SQL It describes and discusses va...

Use of COUNT in SQL It describes and discusses various general methods of expressing constraints, eventually noting that support for "=" with relation operands is sufficient f

Declaring exceptions - user-defined exceptions, Declaring Exceptions T...

Declaring Exceptions The Exceptions can be declared only in the declarative part of the PL/SQL subprogram, block, or package. By introducing its name, you can declare an excep

Fetching across commits, Fetching Across Commits The FOR UPDATE clause...

Fetching Across Commits The FOR UPDATE clauses acquire exclusive all row locks. All rows are locked when you open the cursor, and when you commit your transaction they are unl

Declaring and initializing objects in pl/sql, Declaring and Initializing Ob...

Declaring and Initializing Objects: An object type is once defined and installed in the schema; you can use it to declare the objects in any PL/SQL, subprogram, block or packa

Updating variables, Updating Variables For assignment, SQL uses the ke...

Updating Variables For assignment, SQL uses the key word SET, as in SET X = X + 1 (read as "set X equal to X+1") rather than X: = X + 1 as found in many computer languages.

Updating tables in sql, Updating Tables in SQL The topic of updating b...

Updating Tables in SQL The topic of updating by describing the assignment operator, ":=" in Tutorial D. SQL uses a different syntax for assignment, using the key word SET and

Structure of an object type in pl/sql, Structure of an Object Type: Si...

Structure of an Object Type: Similar to package, an object type has 2 parts: the specification and the body. The specification is the interface to your applications; it declar

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd