PL/SQL异常处理方法
PL/SQL异常处理方法1:什么是异常处理:PL/SQL提供一个功能去处理异常,在PL/SQL块中叫做异常处理,使用异常处理我们能够测试代码和避免异常退出。PL/SQL异常信息包含三个部分:1:异常类型2:错误代码3:错误信息通过处理异常我们能够确保PL/SQL块不突然的异常退出。2:异常处理的结构DECLAREDeclaration sectionBEGINException sectionEXCEPTIONWHEN ex_name1 THEN-Error handling statementsWHEN ex_name2 THEN-Error handling statementsWHEN Others THEN-Error handling statementsEND;例子:when others 能够截取所有的异常,一般放在异常处理的最后。以上代码解释: 当异常出现的时候,oracle寻找合适的when的异常类型执行异常。异常处理的好例子: 除0异常:[sql]SQL> DECLARE2 stock_price NUMBER := 9.73;3 net_earnings NUMBER := 0;4 pe_ratio NUMBER;5 BEGIN6 -- 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 begin10 -- Only one of the WHEN blocks is executed.11 WHEN ZERO_DIVIDE THEN -- handles 'division by zero' error12 DBMS_OUTPUT.PUT_LINE('Company must have had zero earnings.');13 pe_ratio := NULL;14 WHEN OTHERS THEN -- handles all other errors15 DBMS_OUTPUT.PUT_LINE('Some other kind of error occurred.');16 pe_ratio := NULL;17 END; -- exception handlers and block end here18 /PL/SQL 过程已成功完成。SQL> set serveroutput onSQL> /Company must have had zero earnings.为了避免除0可以这样:[sql]DECLAREstock_price NUMBER := 9.73;net_earnings NUMBER := 0;pe_ratio NUMBER;BEGINpe_ratio :=CASE net_earningsWHEN 0 THEN NULLELSE stock_price / net_earningsend;END;/如果有一个嵌套的PL/SQL块像这样:DELCAREDeclaration sectionBEGINDECLAREDeclaration sectionBEGINExecution sectionEXCEPTIONException sectionEND;EXCEPTIONException sectionEND;在以上例子中: 如果异常出现在内部的块中,内部异常处理块应该处理这个异常,如果内部处理块没有处理这个异常,控制会转移它的上一级的PL/SQL块中,如果上一级也没有对应的异常处理块,程序将错误的结束。3:异常的类型:异常有三种:a:系统命名的异常b:系统未命名的异常c:用户自定义的异常(这个没用过)a:系统命名的异常:当程序违反关系型数据库规则的时候,oracle的系统异常会自动出现,有一些系统异常出现的比较频繁,它们是预定义异常,这样的异常都有一个名字。例如:NO_DATA_FOUND 和 ZERO_DIVIDE都有名字的系统异常。常见的有:Oracle Exception Name Oracle Error ExplanationDUP_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. 以上的例子中有关于命名的异常的使用方法。这里不再说了。BEGINExecution sectionEXCEPTIONWHEN NO_DATA_FOUND THENdbms_output.put_line ('A SELECT...INTO did not return any row.');END;b:未命名系统异常:这些系统异常没有名字,这些异常不经常出现,这些异常有错误代码和关联信息。有两种方式处理未命名的异常:方法一: 使用WHEN OTHERS 异常处理方法二:给一个异常 关联异常代码和名称 ,然后像命名异常一样使用它。方法一异常没有目标性,下面说明方法二:使用Pragma 调用 EXCEPTION_INIT关联一个预定义的oracle错误号到程序定义的异常下面是个demo:DECLAREexception_name EXCEPTION;PRAGMAEXCEPTION_INIT (exception_name, Err_code);BEGINExecution sectionEXCEPTIONWHEN exception_name THENhandle the exceptionEND;先声明异常名称,然后调用EXCEPTION_INIT函数绑定错误号码和异常名上一个:Oracle 11g Undo表空间切换
下一个:RMAN进行备份恢复
- 更多SQLServer疑问解答:
- 配置MSSQL复制指定快照文件夹提示:不是有效的路径或文件名
- 详细解读varchar和Nvarchar区别
- SQL SERVER 2005 同步复制技术
- 进程未能大容量复制到表 解决方法
- MSSql实例教程:MSSql数据库同步
- SQLServer2000同步复制技术实现(分发和订阅)
- sqlserver
- SQLSERVER
- 测试 sqlserver 最大用户数连接
- 写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的
- sqlserver2008的安装问题。
- 为什么安装的SQL server 2008我的没有MSSQLSERVER协议啊?
- 怎样把exel表里的数据复制到sqlserver表里
- sqlserver数据库主键和外键问题
- sqlserver 中的左表连接查询和右表连接查询有啥不同?有什么用?