Ticker

6/recent/ticker-posts

PL/SQL - EXCEPTIONS in PL/SQL

EXCEPTIONS in PL/SQL

EXCEPTIONS

An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions.

Exceptions are situations which must be avoided during program executions. Exceptions are caused by errors, invalid inputs or invalid processing. Exceptions can lead to either program termination or generating unexpected outputs.

PL/SQL provides a feature to handle the Exceptions which occur in a PL/SQL Block known as exception Handling. Using Exception one can test the code and avoid it from exiting abruptly. When an occur a message which explains its cause is received.

PL/SQL Exception message consists of three parts:

1) Type of exception,

2) An error code,

3) A message.

By Handling the exceptions one can ensure a PL/SQL block does not exit abruptly.

Structure of Exception Handling

The General Syntax for coding the exception section is shown

DECLARE

Declaration section

BEGIN

Exception section

EXCEPTION

WHEN ex_name I THEN

—Error handling statements

WHEN ex_name2 THEN

—Error handling statements

WHEN Others THEN

— Error handling statements

END;

General PL/SQL statements can be used in the Exception Block.

When an exception is raised, Oracle searches for an appropriate exception handler in the exception section.