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;


  • 相关阅读:
    完美串(区间dp)
    Brackets(区间dp)
    Eureka的高可用
    在Spring Boot中使用 @ConfigurationProperties 注解
    Spring Boot干货系列:(四)开发Web应用之Thymeleaf篇
    luogu3707 相关分析 (线段树)
    luogu3380/bzoj3196 二逼平衡树 (树状数组套权值线段树)
    bzoj4504 K个串 (优先队列+主席树)
    bzoj4336 骑士的旅行 (树链剖分+multiset)
    suoi37 清点更多船只 (卡空间线段树)
  • 原文地址:https://www.cnblogs.com/ungshow/p/1537600.html
Copyright © 2011-2022 走看看