oracle study notes 6: pl / sql exception error handling

In java programming, we often throw a variety of exceptions, program execution from the next, when an exception occurs, and we do not deal with, it will error, the program terminates, so we in java use throw and try / catch to handle exceptions, which will be pl / sql sql statement as a block, when there is an exception, it will cause the entire block can not run, so we need to be exception handling.

There are three types of exception error in pl / sql in:

1. Predefined error

2. Non-predefined error

3. User-defined error

Exception handling block portion is typically placed in the second half, with the structure:

DECLARE 
  the begin 
-    block 
  Exception
   When first_exception (abnormality names) the then exception handling statements;
   When second_exception (abnormality names) the then exception handling statements;
   When Others the then exception handling statements;
 End ;

Predefined Error: the error oracle predefined exception of 24, treatment of such abnormal condition, need to define in the program, which is automatically triggered by the oracle

For the above exceptions handling, direct reference exception name and can be processed.

For example:

declare
  v_row emp%rowtype;
  begin
  select * into v_row from emp where job='CLERK';
  exception
  when no_data_found then
  dbms_output.put_line('表中没有该数据');
  when TOO_MANY_ROWS then
  dbms_output.put_line('有多条数据');
  when others then
  dbms_output.put_line('111');
end;

2. Non-predefined exception handling

For such a process abnormal condition, we must first define the following steps:

1. Definitions abnormal condition pl / sal block definitions section:

Exceptions EXCEPTION;

2. The oracle defined anomalies and errors linked to the use of exception_init statement:

PRAGMA EXCEPTION_INIT ( < exceptions > , < error code > );

3. To make the appropriate treatment for the exceptions in the exception processing section pl / sql of.

for example:

declare
  dept_not_found exception ;
  pragma exception_init (dept_not_found ,-2291);
  begin
  update emp set deptno=90 where empno=7788;
  exception
  when dept_not_found then
  dbms_output.put_line('该部门不存在');
end;

3. User-defined error

The user can custom exception handling, for example:

declare
  empno_not_found exception ;
  begin
  delete from emp where empno=987987;
  if sql%notfound then
    raise empno_not_found;
  end if;
  exception
    when empno_not_found then
  dbms_output.put_line('找不到该员工');
end;

 

Guess you like

Origin www.cnblogs.com/Zs-book1/p/11228395.html