procedure raise_application_error(num binary_integer, msg varchar2);Arguments:num A variable or literal number that identifies a unique, user-defined error number for this type of error and must fall within the range of: -20000 to -20999 An application that has invoked a PL/SQL block or stored procedure which raises an application error will capture this error number in the program structure:  sqlca.sqlerrmsg A variable or literal string that denotes the error description. An application that has invoked a PL/SQL block or stored procedure which raises an application error will capture this error message in the program structure: sqlca.sqlerrm.sqlerrmcDescription:RAISE_APPLICATION_ERROR terminates the PL/SQL execution, rolls back any DML commands, and generates a run-time error that promotes the same behavior as an Oracle run-time error. The call to RAISE_APPLICATION_ERROR takes a user-defined error number and a text string. User-defined application error numbers must fall within -20000 and -20999. Each error number must have an associated message text string. Consider PL/SQL code that is invoked from a third generation language using the Oracle Pro* precompiler. The PL/SQL code can return a status in the output parameter or a bind variable. Such a status might be the return result of a function or an out mode procedure parameter. But a severe error might warrant the “raising” of an application error back to the application. The paradigm for status and return codes versus raising application errors is quite different. Status and return codes are examined immediately after the reference to PL/SQL - they are typical error and warning conditions. In contrast to this application errors raised with RAISE_APPLICATION_ERROR are handled in the same code fragment as Oracle error handling - these are serious errors.  The application distinguishes a true Oracle error from an application error from the range of the error number.You cannot raise a PL/SQL exception across languages; that is, you cannot raise a PL/SQL exception and have it captured by a calling Pro*C program - C has no exception handler mechanism, but even those languages that do, such as Ada, cannot declare a handler for a PL/SQL exception without special language pragmas. However, you can achieve the same paradigm with RAISE_APPLICATION_ERROR.Consider a trigger that detects the violation of a business rule. For example a trigger in the EMP table might consider the SUM of all employee salaries to exceed the company annual budget. This error detection can be handled by RAISE_APPLCIATION_ERROR from within the trigger. While triggers cannot execute transaction control, that is, they cannot commit, rollback, etc., they can force a rollback and force an error by calling RAISE_APPLICATION_ERROR. In this case the calling program, the program that invoked the DML INSERT or UPDATE can receive the application error code and error message by inspecting the application SQLCA after the INSERT, UPDATE statement.
Within the context of PL/SQL, a stored procedure can capture the result of an application error through an exception handler (a third generation language Pro* program must capture it through the SQLCA) that is mapped to the exception handler. For example, if procedure A calls procedure B and B contains the code:RAISE_APPLICATION_ERROR(-20100, ‘Invalid Registration’);then procedure A can map this error to an exception handler as follows:PROCEDURE B IS
   invalid_data EXCEPTION;
   PRAGMA EXCEPTION_INIT(invalid_data, -20100);
BEGIN
   .. call procedure B which may raise the error;
EXCEPTION
   WHEN invalid_data then
      .. handle this error...
END;

解决方案 »

  1.   

    上面不是说了吗?用RAISE_APPLICATION_ERROR(num,msg)
    num在-20000到-20999之间,msg写你希望抛出的异常。
      

  2.   

    create or replace trigger BINSERT_CADETAIL
      before insert on t_cadetail  
      for each row
    declare
      num number(1);
    begin
      select count(*) into num from t_cadetail t where t.cano=:NEW.cano and t.caequipmentid=:NEW.caequipmentid and t.state!='X' and t.state!='D';
      if num>0 then
         RAISE_APPLICATION_ERROR(-20100, 'Invalid Registration');
      end if;
    end BINSERT_CADETAIL;
    这样写对吗?