📜  PL / SQL-异常

📅  最后修改于: 2020-11-26 05:58:57             🧑  作者: Mango


在本章中,我们将讨论PL / SQL中的异常。异常是程序执行期间的错误条件。 PL / SQL支持程序员使用程序中的EXCEPTION块来捕获此类条件,并针对错误条件采取适当的措施。有两种类型的例外-

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

异常处理的语法

异常处理的一般语法如下。在这里,您可以列出尽可能多的例外。默认异常将在其他情况下使用THEN-

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表-

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提示符下执行时,将产生以下结果-

No such customer!  

PL/SQL procedure successfully completed. 

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

引发异常

每当发生内部数据库错误时,数据库服务器都会自动引发异常,但是程序员可以使用命令RAISE显式引发异常。以下是引发异常的简单语法-

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

您可以使用上述语法来引发Oracle标准异常或任何用户定义的异常。在下一节中,我们将为您提供引发用户定义异常的示例。您可以通过类似的方式提出Oracle标准异常。

用户定义的异常

PL / SQL允许您根据程序的需要定义自己的异常。必须声明一个用户定义的异常,然后使用RAISE语句或过程DBMS_STANDARD.RAISE_APPLICATION_ERROR显式引发该异常。

声明异常的语法是-

DECLARE 
   my-exception EXCEPTION; 

以下示例说明了该概念。该程序要求客户ID,当用户输入无效ID时,将引发异常invalid_id

DECLARE 
   c_id customers.id%type := &cc_id; 
   c_name customerS.Name%type; 
   c_addr customers.address%type;  
   -- user defined exception 
   ex_invalid_id  EXCEPTION; 
BEGIN 
   IF c_id <= 0 THEN 
      RAISE ex_invalid_id; 
   ELSE 
      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); 
   END IF; 

EXCEPTION 
   WHEN ex_invalid_id THEN 
      dbms_output.put_line('ID must be greater than zero!'); 
   WHEN no_data_found THEN 
      dbms_output.put_line('No such customer!'); 
   WHEN others THEN 
      dbms_output.put_line('Error!');  
END; 
/

当以上代码在SQL提示符下执行时,将产生以下结果-

Enter value for cc_id: -6 (let's enter a value -6) 
old  2: c_id customers.id%type := &cc_id; 
new  2: c_id customers.id%type := -6; 
ID must be greater than zero! 
 
PL/SQL procedure successfully completed. 

预定义例外

PL / SQL提供了许多预定义的异常,当程序违反任何数据库规则时将执行这些异常。例如,当SELECT INTO语句不返回任何行时,将引发预定义的异常NO_DATA_FOUND。下表列出了一些重要的预定义例外-

Exception Oracle Error SQLCODE 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 clause 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 a 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 sizeconstraint error occurs.
ZERO_DIVIDE 01476 1476 It is raised when an attempt is made to divide a number by zero.