📜  PL / SQL异常

📅  最后修改于: 2020-11-12 01:52:08             🧑  作者: Mango

PL / SQL异常处理

什么是例外

程序执行期间发生错误,在PL / SQL中称为“异常”。

PL / SQL有助于程序员使用程序中的异常块来捕获此类条件,并针对错误条件采取适当的措施。

有两种类型的例外:

  • 系统定义的异常
  • 用户定义的异常

PL / SQL异常处理

异常处理的语法:

以下是异常处理的一般语法:

DECLARE
   
BEGIN
   
EXCEPTION
   
   WHEN exception1 THEN 
       exception1-handling-statements 
   WHEN exception2  THEN 
      exception2-handling-statements 
   WHEN exception3 THEN 
      exception3-handling-statements
   ........
   WHEN others THEN
      exception3-handling-statements
END;

异常处理示例

让我们以一个简单的示例来演示异常处理的概念。在这里,我们使用已经创建的CUSTOMERS表。

从客户中选择*;

ID NAME AGE ADDRESS SALARY
1 Ramesh 23 Allahabad 20000
2 Suresh 22 Kanpur 22000
3 Mahesh 24 Ghaziabad 24000
4 Chandan 25 Noida 26000
5 Alex 21 Paris 28000
6 Sunita 20 Delhi 30000
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;
/ 

在SQL Prompt上执行上述代码后,将产生以下结果:


No such customer!
PL/SQL procedure successfully completed.

上面的程序应显示给出ID的客户的姓名和地址。但是我们的数据库中没有ID值为8的客户,因此该程序引发运行时异常NO_DATA_FOUND,该异常在EXCEPTION块中捕获。

注意:因为上面的示例中使用的customer_id为8,并且该表中没有id为8的用户,您将获得结果“ No such customer”。

如果使用上表中定义的ID(即1到6),则会得到一定的结果。对于演示示例:在这里,我们使用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;
/ 

在SQL提示符下执行上述代码后,您将获得以下结果:


Name: alex
Address: paris
PL/SQL procedure successfully completed.

引发异常

如果发生任何内部数据库错误,则数据库服务器会自动引发异常。但是程序员也可以使用命令RAISE显式地提高它。

引发异常的语法:

DECLARE
   exception_name EXCEPTION;
BEGIN
   IF condition THEN
      RAISE exception_name;
   END IF;
EXCEPTION
   WHEN exception_name THEN
   statement;
END;

PL / SQL用户定义的异常

PL / SQL方便其用户根据程序需要定义自己的异常。可以使用RAISE语句或过程DBMS_STANDARD.RAISE_APPLICATION_ERROR显式引发用户定义的异常。

用户定义异常的语法

DECLARE
my-exception EXCEPTION; 

PL / SQL预定义异常

当程序违反任何数据库规则时,就会执行PL / SQL中许多预定义的异常。

例如:NO_DATA_FOUND是一个预定义的异常,当SELECT INTO语句不返回任何行时会引发此异常。

以下是一些重要的预定义例外的列表:

Exception Oracle Error SQL Code Description
ACCESS_INTO_NULL 06530 -6530 It is raised when a NULL object is automatically assigned a value.
CASE_NOT_FOUND 06592 -6592 It is raised when none of the choices in the “WHEN” clauses of a CASE statement is selected, and there is no else clause.
COLLECTION_IS_NULL 06531 -6531 It is raised when a program attempts to apply collection methods other than exists to an uninitialized nested table or varray, or the program attempts to assign values to the elements of an uninitialized nested table or varray.
DUP_VAL_ON_INDEX 00001 -1 It is raised when duplicate values are attempted to be stored in a column with unique index.
INVALID_CURSOR 01001 -1001 It is raised when attempts are made to make a cursor operation that is not allowed, such as closing an unopened cursor.
INVALID_NUMBER 01722 -1722 It is raised when the conversion of a character string into a number fails because the string does not represent a valid number.
LOGIN_DENIED 01017 -1017 It is raised when s program attempts to log on to the database with an invalid username or password.
NO_DATA_FOUND 01403 +100 It is raised when a select into statement returns no rows.
NOT_LOGGED_ON 01012 -1012 It is raised when a database call is issued without being connected to the database.
PROGRAM_ERROR 06501 -6501 It is raised when PL/SQL has an internal problem.
ROWTYPE_MISMATCH 06504 -6504 It is raised when a cursor fetches value in a variable having incompatible data type.
SELF_IS_NULL 30625 -30625 It is raised when a member method is invoked, but the instance of the object type was not initialized.
STORAGE_ERROR 06500 -6500 It is raised when PL/SQL ran out of memory or memory was corrupted.
TOO_MANY_ROWS 01422 -1422 It is raised when a SELECT INTO statement returns more than one row.
VALUE_ERROR 06502 -6502 It is raised when an arithmetic, conversion, truncation, or size-constraint error occurs.
ZERO_DIVIDE 01476 1476 It is raised when an attempt is made to divide a number by zero.