Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
3
Добавлен:
01.07.2025
Размер:
5 Мб
Скачать
☆

4.3.1.3 The target label must be in the same part of the pl/sql block as the goto

The target label must be in the same part of the PL/SQL block as the GOTO statement.A GOTO in the execution section may not go to a label in the exception section; a GOTO in the exception section may not go to a label in the execution section. A GOTO in an exception handler may reference a label in the same handler. The following code example generates the same PL/SQL error shown in the previous section (PLS-00375):

BEGIN

/*

|| The label and GOTO must be in the same section!

*/

GOTO out_of_here;

EXCEPTION

WHEN OTHERS

THEN

<<out_of_here>> /* Out of scope! */

NULL;

END;

4.4 The null Statement

Usually when you write a statement in a program, you want it to do something. There are cases, however, when you want to tell PL/SQL to do absolutely nothing, and that is where the NULL statement comes in handy. The NULL statement has the following format:

NULL;

Well, you wouldn't want ado-nothing statement to be complicated, would you? The NULL statement is simply the reserved word NULL followed by a semicolon (;) to indicate that this is a statement and not a NULL value. The NULL statement does nothing except pass control to the next executable statement.

Why would you want to use the NULL statement? There are several reasons, described in the following sections.

4.4.1 Improving Program Readability

There are many situations in your program where you logically do not want to take any action. In most of these cases, PL/SQL will let you write nothing and the program will execute as you wish. The only drawback is the ambiguity surrounding this solution: it is not clear to a person examining the program that you purposely took no action.

For example, when you write an IF statement, you do not have to include an ELSE clause. To produce a report based on a selection, you can code:

IF :report.selection = 'DETAIL'

THEN

exec_detail_report;

END IF;

What should the program be doing if the report selection is not `DETAIL'? One would assume that the program is supposed to do nothing. But because this is not explicitly stated in the code, one is left to wonder if perhaps there was an oversight. If, on the other hand, you include an explicit ELSE clause that does nothing, you state very clearly, "Don't worry, I thought about this possibility and I really want nothing to happen."

IF :report.selection = 'DETAIL'

THEN

exec_detail_report;

ELSE

NULL; -- Do nothing

END IF;

4.4.2 Nullifying a Raised Exception

The optional exception section of a program contains one or more exception handlers. These handlers trap and handle errors that have been raised in your program. The structure and flow of the exception section is similar in structure and flow to a conditional CASE statement, as follows:

EXCEPTION

WHEN exception_name1

THEN

executable_statements;

WHEN exception_nameN

THEN

executable_statements;

WHEN OTHERS

THEN

executable_statements;

END;

If exception_name1 is raised, then execute its statements; if exception_nameN is raised, then execute its statements; and so on. The WHEN OTHERS clause handles any exceptions not handled in the previous WHEN clauses (it is just like the ELSE clause of the IF statement). When you don't want to write any special code to handle an exception, you can use the NULL statement to make sure that a raised exception halts execution of the current PL/SQL block, but does not propagate any exceptions to enclosing blocks:

PROCEDURE calc_avg_sales

BEGIN

:sales.avg := :sales.month1 / :sales.total;

EXCEPTION

WHEN ZERO_DIVIDE

THEN

:sales.avg := 0;

RAISE FORM_TRIGGER_FAILURE;

WHEN OTHERS

THEN

NULL;

END;

If total sales are zero, then an exception is raised, the average is set to zero, and the trigger processing in Oracle Forms is halted. If any other exceptions occur (such as VALUE_ERROR, which would be raised if the number generated by the calculation is larger than the sales.avg item allows), the WHEN OTHERS clause gets control, handles the exception by doing nothing (i.e., executes the NULL statement), and processing continues. Because the exception is handled, it is not raised to the enclosing block.

See Chapter 6 for more detailed information about exceptions.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]