C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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: DECLARE 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;
DECLARE c_id customers.id%type := 8; c_name customers.name%type; c_addr customers.address%type; BEGIN SELECT name, address INTO c_name, c_addr FROM customers WHERE id = c_id; DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name); DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr); EXCEPTION WHEN no_data_found THEN dbms_output.put_line('No such customer!'); WHEN others THEN dbms_output.put_line('Error!'); END; / 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. DECLARE c_id customers.id%type := 5; c_name customers.name%type; c_addr customers.address%type; BEGIN SELECT name, address INTO c_name, c_addr FROM customers WHERE id = c_id; DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name); DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr); EXCEPTION WHEN no_data_found THEN dbms_output.put_line('No such customer!'); WHEN others THEN dbms_output.put_line('Error!'); END; / 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: DECLARE exception_name EXCEPTION; BEGIN IF condition THEN RAISE exception_name; END IF; EXCEPTION WHEN exception_name THEN statement; END; 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 DECLARE my-exception EXCEPTION; 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
|