当前位置:数据库 > SQLServer >>

PL/SQL异常处理方法

PL/SQL异常处理方法
 
1:什么是异常处理:
PL/SQL提供一个功能去处理异常,在PL/SQL块中叫做异常处理,使用异常处理我们能够测试代码和避免异常退出。
PL/SQL异常信息包含三个部分:
      1:异常类型
       2:错误代码
       3:错误信息
通过处理异常我们能够确保PL/SQL块不突然的异常退出。
 
2:异常处理的结构
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; 
例子:
when others 能够截取所有的异常,一般放在异常处理的最后。
以上代码解释: 当异常出现的时候,oracle寻找合适的when的异常类型执行异常。
 
异常处理的好例子: 除0异常:
[sql] 
SQL> DECLARE  
  2     stock_price NUMBER := 9.73;  
  3     net_earnings NUMBER := 0;  
  4     pe_ratio NUMBER;  
  5  BEGIN  
  6  -- Calculation might cause division-by-zero error.  
  7     pe_ratio := stock_price / net_earnings;  
  8     DBMS_OUTPUT.PUT_LINE('Price/earnings ratio = ' || pe_ratio);  
  9  EXCEPTION  -- exception handlers begin  
 10  -- Only one of the WHEN blocks is executed.  
 11     WHEN ZERO_DIVIDE THEN  -- handles 'division by zero' error  
 12        DBMS_OUTPUT.PUT_LINE('Company must have had zero earnings.');  
 13        pe_ratio := NULL;  
 14     WHEN OTHERS THEN  -- handles all other errors  
 15        DBMS_OUTPUT.PUT_LINE('Some other kind of error occurred.');  
 16        pe_ratio := NULL;  
 17  END;  -- exception handlers and block end here  
 18  /  
  
PL/SQL 过程已成功完成。  
  
SQL> set serveroutput on  
SQL> /  
Company must have had zero earnings.  
为了避免除0可以这样:
[sql] 
DECLARE  
   stock_price NUMBER := 9.73;  
   net_earnings NUMBER := 0;  
   pe_ratio NUMBER;  
BEGIN  
   pe_ratio :=  
      CASE net_earnings  
         WHEN 0 THEN NULL  
         ELSE stock_price / net_earnings  
      end;  
END;  
/  
 
 
如果有一个嵌套的PL/SQL块像这样:
 DELCARE
   Declaration section 
 BEGIN
    DECLARE
      Declaration section 
    BEGIN 
      Execution section 
    EXCEPTION 
      Exception section 
    END; 
 EXCEPTION
   Exception section 
 END; 
 
在以上例子中: 如果异常出现在内部的块中,内部异常处理块应该处理这个异常,如果内部处理块没有处理这个
异常,控制会转移它的上一级的PL/SQL块中,如果上一级也没有对应的异常处理块,程序将错误的结束。
 
3:异常的类型:
异常有三种: 
  a:系统命名的异常
  b:系统未命名的异常
  c:用户自定义的异常(这个没用过)
 
a:系统命名的异常:
当程序违反关系型数据库规则的时候,oracle的系统异常会自动出现,有一些系统异常出现的比较频繁,它们是预定义
异常,这样的异常都有一个名字。
例如:NO_DATA_FOUND 和 ZERO_DIVIDE都有名字的系统异常。
常见的有:
Oracle Exception Name Oracle Error Explanation
DUP_VAL_ON_INDEX ORA-00001 You tried to execute an INSERT or UPDATE statement that has created a duplicate value in a field restricted by a unique index.
TIMEOUT_ON_RESOURCE ORA-00051 You were waiting for a resource and you timed out.
TRANSACTION_BACKED_OUT ORA-00061 The remote portion of a transaction has rolled back.
INVALID_CURSOR ORA-01001 You tried to reference a cursor that does not yet exist. This may have happened because you've executed a FETCH cursor or CLOSE cursor before OPENing the cursor.
NOT_LOGGED_ON ORA-01012 You tried to execute a call to Oracle before logging in.
LOGIN_DENIED ORA-01017 You tried to log into Oracle with an invalid username/password combination.
NO_DATA_FOUND ORA-01403 You tried one of the following:
You executed a SELECT INTO statement and no rows were returned.
You referenced an uninitialized row in a table.
You read past the end of file with the UTL_FILE package.
TOO_MANY_ROWS ORA-01422 You tried to execute a SELECT INTO statement and more than one row was returned.
ZERO_DIVIDE ORA-01476 You tried to divide a number by zero.
INVALID_NUMBER ORA-01722 You tried to execute an SQL statement that tried to convert a string to a number, but it was unsuccessful.
STORAGE_ERROR ORA-06500 You ran out of memory or memory was corrupted.
PROGRAM_ERROR ORA-06501 This is a generic "Contact Oracle support" message because an internal problem was encountered.
VALUE_ERROR ORA-06502 You tried to perform an operation and there was a error on a conversion, truncation, or invalid constraining of numeric or character data.
CURSOR_ALREADY_OPEN ORA-06511 You tried to open a cursor that is already open. 以上的例子中有关于命名的异常的使用方法。这里不再说了。
BEGIN 
  Execution section
EXCEPTION 
WHEN NO_DATA_FOUND THEN 
 dbms_output.put_line ('A SELECT...INTO did not return any row.'); 
 END; 
 
b:未命名系统异常:
这些系统异常没有名字,这些异常不经常出现,这些异常有错误代码和关联信息。
有两种方式处理未命名的异常:
        方法一: 使用WHEN OTHERS 异常处理
        方法二:给一个异常 关联异常代码和名称 ,然后像命名异常一样使用它。
方法一异常没有目标性,下面说明方法二:
使用Pragma 调用 EXCEPTION_INIT关联一个预定义的oracle错误号到程序定义的异常
下面是个demo:
DECLARE 
   exception_name EXCEPTION; 
   PRAGMA 
   EXCEPTION_INIT (exception_name, Err_code); 
BEGIN 
Execution section
EXCEPTION
  WHEN exception_name THEN
     handle the exception
END;
先声明异常名称,然后调用EXCEPTION_INIT函数绑定错误号码和异常名
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,