Oracle database process-exception handling

table of Contents

 

table of Contents

1. The benefits of using exceptions correctly

2. Exception handling

2.1. Custom exception

2.2. Predefined exception handling

2.3. Use predefined exceptions


1. The benefits of using exceptions correctly

When an error occurs in the program, it can accurately locate where the error occurred and the cause of the error.

2. Exception handling

2.1. Custom exception

  1. First, the exception needs to be declared. Reference: e_bade_value EXCEPTION;
  2. In the execution process, it needs to be judged, and then throw an exception message. Reference: RAISE e_bade_value;

After the RAISE e_bade_value; line is executed, it will automatically jump to the Exception execution statement to capture and handle the exception in the Exception.
 

-- Created on 2019/4/19 by ADMINISTRATOR 
declare 
 v_companycode          ggcompany.companycode%type;
 v_new_companycode      ggcompany.companycode%type;
 e_bade_value           EXCEPTION;
begin
  v_new_companycode := '1573400000';
  select gg.companycode into v_companycode from ggcompany gg where gg.companycode = '1573400000';
  if v_new_companycode = v_companycode Then
    RAISE e_bade_value;
  end if;
Exception 
    when e_bade_value
      then 
        Dbms_Output.put_line('自定义异常成功');
    when Others
      then 
        Dbms_Output.put_line('公共异常');
end;

2.2. Predefined exception handling

  There is another way of writing in the predefined exception handling, and the reference for writing is RASIE_APPLICATION_ERROR.
 

语法: RASIE_APPLICATION_ERROR(error_number, error_message, [keep_errors]);

error_number: a negative number ranging from -20000 to -20999
error_message: a string with a maximum length of 2048 bytes
keep_error: an optional boolean value, when the value is True, a new error will be added to the already thrown In the error list, the default value is False, which replaces the current error list with the new error.

 Note: RASIE_APPLICATION_ERROR can only be called in a stored subroutine. When called, it will end the current subroutine and return a user-defined error code and error message to the application. These error codes and error messages can be used by any Oracle The same error is caught.

-- Created on 2019/4/19 by ADMINISTRATOR 
declare 
 v_companycode          ggcompany.companycode%type;
 v_new_companycode      ggcompany.companycode%type;
 e_bade_value           EXCEPTION;
begin
  --v_new_companycode := null;
  if v_new_companycode is null Then
        raise_application_error(-20000, '员工编号不能为空'); --触发应用程序异常
  ELSE
   select gg.companycode into v_companycode from ggcompany gg where gg.companycode = '1573400000';
   if v_new_companycode = v_companycode Then
    RAISE e_bade_value;
   end if;
  End if;
Exception 
    when e_bade_value
      then 
        Dbms_Output.put_line('自定义异常成功');
    when Others
      then 
        Dbms_Output.put_line(sqlerrm);
end;

2.3. Use predefined exceptions

Predefined exception information
Error number Exception error message name Description
ORA-0001 Dup_val_on_index Violation of uniqueness restriction
ORA-0051 Timeout-on-resource A timeout occurred while waiting for a resource
ORA-0061 Transaction-backed-out The transaction was cancelled due to a deadlock
ORA-1001 Invalid-CURSOR Attempt to use an invalid cursor
ORA-1012 Not-logged-on Not connected to ORACLE
ORA-1017 Login-denied Invalid username/password
ORA-1403 No_data_found SELECT INTO did not find data
ORA-1422 Too_many_rows SELECT INTO returns multiple rows
ORA-1722 Invalid-NUMBER Failed to convert a number
ORA-6500 Storage-error Internal error caused by insufficient memory
ORA-6501 Program-error internal error
ORA-6502 Value-error Conversion or truncation error
ORA-6504 Rowtype-mismatch Host cursor variables and PL/SQL variables have incompatible row types
ORA-6511 CURSOR-already-OPEN Attempt to open a cursor that is already open
ORA-6530 Access-INTO-null Attempt to assign a value to a property of a null object
ORA-6531 Collection-is-null Attempt to apply a collection method other than Exists to a null pl/sql table or varray
ORA-6532 Subscript-outside-limit References to nested or varray indexes are outside the scope of the declaration
ORA-6533 Subscript-beyond-count Reference to nested or varray index is greater than the element in the collection

Guess you like

Origin blog.csdn.net/baidu_31572291/article/details/101377538