Exception handling, PL-SQL Programming

Exception handling

In the PL/SQL, a warning or error condition is known as an exception. The Exceptions can be internally defined (by the run-time system) or user defined. The Examples of internally defined exceptions involve division by zero and out of memory. Some familiar internal exceptions have predefined names, like ZERO_DIVIDE and STORAGE_ERROR.

You can define exceptions of your own in the declarative part of any PL/SQL subprogram, block, or package. For illustration, you might define an exception namely the insufficient_funds to flag overdrawn bank accounts. Dissimilar internal exceptions, user-defined exceptions should be given names.

Whenever errors occur, an exception is raised. That is, the normal execution stops and control transfers to the exception-handling section of your PL/SQL subprogram or block. The Internal exceptions are raised implicitly (automatically) by the run-time system. The User-defined exceptions should be raised explicitly by the RAISE statements that can also raise the predefined exceptions.

To handle the raised exceptions, you write individual routines known as the exception handlers.

Later an exception handler runs, the present block stops executing and the enclosing block resumes with the next statement. If there is no enclosing block, the control returns to the host atmosphere.

In the illustration below, you compute and store a price-to-earnings ratio for a company with ticker symbol XYZ. The predefined exception ZERO_DIVIDE is raised whenever the company has zero earnings. This stops general execution of the block and transfers control to the exception handlers. The elective OTHERS handler catches all the exceptions which the block does not name explicitly.

DECLARE

pe_ratio NUMBER(3,1);

BEGIN

SELECT price / earnings INTO pe_ratio FROM stocks

WHERE symbol = 'XYZ'; -- might cause division-by-zero error

INSERT INTO stats (symbol, ratio) VALUES ('XYZ', pe_ratio);

COMMIT;

EXCEPTION -- exception handlers begin

WHEN ZERO_DIVIDE THEN -- handles 'division by zero' error

INSERT INTO stats (symbol, ratio) VALUES ('XYZ', NULL);

COMMIT;

...

WHEN OTHERS THEN -- handles all other errors

ROLLBACK;

END; -- exception handlers and block end here

The last illustration describes an exception handling, which is not the effective use of INSERT statements. For illustration, an enhanced way to do the insert is as shown:

INSERT INTO stats (symbol, ratio)

SELECT symbol, DECODE(earnings, 0, NULL, price / earnings)

FROM stocks WHERE symbol = 'XYZ';

Posted Date: 10/5/2012 5:04:12 AM | Location : United States







Related Discussions:- Exception handling, Assignment Help, Ask Question on Exception handling, Get Answer, Expert's Help, Exception handling Discussions

Write discussion on Exception handling
Your posts are moderated
Related Questions
When Are Constraints Checked Under the model constraints are conceptually checked at all statement boundaries (and only at statement boundaries). By default the same is true

Perform the following queries on the Hospital1.DB using SQL Anywhere (START EARLY!). a. Which patients have purchased the drug "Tylenol"? List the names and addresses. Arrange the

Data Abstraction The Data abstraction extracts the important properties of data while ignoring the not necessary details. Once you design a data structure, you can fail to reme

Product-specific Packages The Oracle and different Oracle tools are supplied with the product-specific packages which help you to build the PL/SQL-based applications. For illus

Parameter and Keyword Description: package_name: This construct identifies the package. AUTHID Clause: This determine whether all the packaged subprograms impleme

Collections:   The collection is an ordered group of elements, all of similar type (for illustration, the grades for a class of students). Each element has an exclusive subsc

Raise_application_error -  procedure of package DBMS_STANDARD , allows to issue an user_defined error messages by stored sub-program or database trigger.

%NOTFOUND The %NOTFOUND is the logical opposite of the %FOUND. The %NOTFOUND yields TRUE when an INSERT, UPDATE, or DELETE statement affected no rows, or the SELECT INTO state

What are the rates for help in writing PL/SQL procedures and functions?

Due to an increase in overhead costs, the buying price of all items needs to be increased. Management wants to see a report before deciding how much each product will go up. Add to