zoukankan      html  css  js  c++  java
  • oracle exception使用

    1.RAISE_APPLICATION_ERROR的定义及使用

    The procedure RAISE_APPLICATION_ERROR lets you issue user-defined ORA- error messages from stored subprograms. That way, you can report errors to your application and avoid returning unhandled exceptions.

    To call RAISE_APPLICATION_ERROR, use the syntax

    raise_application_error(error_number, message[, {TRUE | FALSE}]);

    where error_number is a negative integer in the range -20000 .. -20999 and message is a character string up to 2048 bytes long. If the optional third parameter is TRUE, the error is placed on the stack of previous errors. If the parameter is FALSE (the default), the error replaces all previous errors. RAISE_APPLICATION_ERROR is part of package DBMS_STANDARD, and as with package STANDARD, you do not need to qualify references to it.

    An application can call raise_application_error only from an executing stored subprogram (or method). When called, raise_application_error ends the subprogram and returns a user-defined error number and message to the application. The error number and message can be trapped like any Oracle error.

    In the following example, you call raise_application_error if an employee's salary is missing:

    CREATE PROCEDURE raise_salary (emp_id NUMBER, amount NUMBER) AS
    curr_sal NUMBER;
    BEGIN
    SELECT sal INTO curr_sal FROM emp WHERE empno = emp_id;
    IF curr_sal IS NULL THEN
    /* Issue user-defined error message. */
    raise_application_error(-20101, 'Salary is missing');
    ELSE
    UPDATE emp SET sal = curr_sal + amount WHERE empno = emp_id;
    END IF;
    END raise_salary;

    2.结合EXCEption_INIT,自定义异常

    The calling application gets a PL/SQL exception, which it can process using the error-reporting functions SQLCODE and SQLERRM in an OTHERS handler. Also, it can use the pragma EXCEPTION_INIT to map specific error numbers returned by raise_application_error to exceptions of its own, as the following Pro*C example shows:

    EXEC SQL EXECUTE
    /* Execute embedded PL/SQL block using host
    variables my_emp_id and my_amount, which were
    assigned values in the host environment. */
    DECLARE
    null_salary EXCEPTION;
    /* Map error number returned by raise_application_error
    to user-defined exception. */
    PRAGMA EXCEPTION_INIT(null_salary, -20101);
    BEGIN
    raise_salary(:my_emp_id, :my_amount);
    EXCEPTION
    WHEN null_salary THEN
    INSERT INTO emp_audit VALUES (:my_emp_id, ...);
    END;
    END-EXEC;


  • 相关阅读:
    DIOCP开源项目详解编码器和解码器和如何在传输中加入压缩和解压功能
    DIOCP开源项目DEMO(怎么样操作远程数据库)
    网站文件更新工具
    使用Javascript正则表达式来格式化XML内容
    加载有命名空间,但没有声名的XML
    使用参数化和块语句来提高批处理SQL语句的执行效率
    让Dotnet识别Java发送来的自定义SoapHeader
    中行的EToken
    异步调用方法时异常的捕获
    使用参数化和块语句来提高批处理SQL语句的执行效率(2)
  • 原文地址:https://www.cnblogs.com/ungshow/p/1537600.html
Copyright © 2011-2022 走看看