Exception
Handling
In this section we will discuss
about the following,
1) What is Exception Handling.
2) Structure of Exception Handling.
3) Types of Exception Handling.
1) What is Exception Handling.
2) Structure of Exception Handling.
3) Types of Exception Handling.
1)
What is Exception Handling?
PL/SQL provides a feature to handle
the Exceptions which occur in a PL/SQL Block known as exception Handling. Using
Exception Handling we can test the code and avoid it from exiting abruptly.
When an exception occurs a messages which explains its cause is recieved.
PL/SQL Exception message consists of three parts.
1) Type of Exception
2) An Error Code
3) A message
By Handling the exceptions we can ensure a PL/SQL block does not exit abruptly.
PL/SQL Exception message consists of three parts.
1) Type of Exception
2) An Error Code
3) A message
By Handling the exceptions we can ensure a PL/SQL block does not exit abruptly.
2)
Structure of Exception Handling.
The General Syntax for coding the
exception section
DECLARE
Declaration section
BEGIN
Exception section
EXCEPTION
WHEN ex_name1 THEN
-Error handling statements
WHEN ex_name2 THEN
-Error handling statements
WHEN Others THEN
-Error handling statements
END;
General PL/SQL statments can be used
in the Exception Block.
When an exception is raised, Oracle
searches for an appropriate exception handler in the exception section. For
example in the above example, if the error raised is 'ex_name1 ', then the
error is handled according to the statements under it. Since, it is not
possible to determine all the possible runtime errors during testing fo the
code, the 'WHEN Others' exception is used to manage the exceptions that are not
explicitly handled. Only one exception can be raised in a Block and the control
does not return to the Execution Section after the error is handled.
If there are nested PL/SQL blocks
like this.
DELCARE
Declaration section
BEGIN
DECLARE
Declaration section
BEGIN
Execution section
EXCEPTION
Exception section
END;
EXCEPTION
Exception section
END;
In the above case, if the exception
is raised in the inner block it should be handled in the exception block of the
inner PL/SQL block else the control moves to the Exception block of the next
upper PL/SQL Block. If none of the blocks handle the exception the program ends
abruptly with an error.
No comments:
Post a Comment