Regardless of whether the program is a procedure, function, or trigger, an SPL program has the same block structure. A block consists of up to three sections - an optional declaration section, a mandatory executable section, and an optional exception section. Minimally, a block has an executable section that consists of one or more SPL statements within the keywords, BEGIN and END
You can include an exception section within the BEGIN
block. The exception section begins with the keyword, EXCEPTION
, and continues until the end of the block in which it appears. If an exception is thrown by a statement within the block, program control goes to the exception section where the thrown exception may or may not be handled depending upon the exception and the contents of the exception section.
WHEN exception_condition THEN
declarations are one or more variable, cursor, or type declarations that are local to the block. Each declaration must be terminated by a semicolon. The use of the keyword DECLARE depends upon the context in which the block appears.
statements are one or more SPL statements. Each statement must be terminated by a semicolon. The end of the block denoted by the keyword END
must also be terminated by a semicolon.
If present, the keyword EXCEPTION
marks the beginning of the exception section. exception_condition
is a conditional expression testing for one or more types of exceptions. If a thrown exception matches one of the exceptions in exception_condition
, the statements
following the WHEN exception_condition
clause are executed. There may be one or more WHEN exception_condition
clauses, each followed by statements
block in itself, is considered a statement; thus, blocks may be nested. The exception section may also contain nested blocks.