91-9990449935 0120-4256464 |
PL/SQL Exception HandlingWhat is ExceptionAn error occurs during the program execution is called Exception in PL/SQL. PL/SQL facilitates programmers to catch such conditions using exception block in the program and an appropriate action is taken against the error condition. There are two type of exceptions:
PL/SQL Exception HandlingSyntax for exception handling: Following is a general syntax for exception handling: Example of exception handlingLet's take a simple example to demonstrate the concept of exception handling. Here we are using the already created CUSTOMERS table.
SELECT* FROM COUSTOMERS;
After the execution of above code at SQL Prompt, it produces the following result: No such customer! PL/SQL procedure successfully completed. The above program should show the name and address of a customer as result whose ID is given. But there is no customer with ID value 8 in our database, so the program raises the run-time exception NO_DATA_FOUND, which is captured in EXCEPTION block. Note: You get the result "No such customer" because the customer_id used in the above example is 8 and there is no cutomer having id value 8 in that table.If you use the id defined in the above table (i.e. 1 to 6), you will get a certain result. For a demo example: here, we are using the id 5. After the execution of above code at SQL prompt, you will get the following result: Name: alex Address: paris PL/SQL procedure successfully completed. Raising ExceptionsIn the case of any internal database error, exceptions are raised by the database server automatically. But it can also be raised explicitly by programmer by using command RAISE. Syntax for raising an exception: PL/SQL User-defined ExceptionsPL/SQL facilitates their users to define their own exceptions according to the need of the program. A user-defined exception can be raised explicitly, using either a RAISE statement or the procedure DBMS_STANDARD.RAISE_APPLICATION_ERROR. Syntax for user define exceptions PL/SQL Pre-defined ExceptionsThere are many pre-defined exception in PL/SQL which are executed when any database rule is violated by the programs. For example: NO_DATA_FOUND is a pre-defined exception which is raised when a SELECT INTO statement returns no rows. Following is a list of some important pre-defined exceptions:
Next TopicPL/SQL Trigger
|