例外

系统定义异常

no_data_found(没有找到数据)

too_many_rows(select...into语句匹配多个行)

zero_divide(被零除)

value_error(算数或转换错误)

timeout_on_resource(在等待资源时发生超时)

范例

--被零除例外
declare
  pnum number;
begin
  pnum := 1/0;
  exception
    when zero_divide then
      dbms_output.put_line('1:0不能做分母');
      dbms_output.put_line('2:0不能做分母');
    when value_error then
      dbms_output.put_line('算数或者转换错误');
    when others then
      dbms_output.put_line('其他例外');
  end;

自定义例外

1. 在declare声明部分使用exception定义例外变量

2. 如果遇到异常使用raise抛出

范例

--查询50号部门的员工
declare
  cursor cemp is select ename from emp where deptno = 50;
  pename emp.ename%type;
  --自定义例外
  no_emp_found exception;
begin
  open cemp;
  fetch cemp into pename;
  if cemp%notfound then
    raise no_emp_found;
  end if;
  close cemp;
  exception 
    when no_emp_found then
      dbms_output.put_line('没有找到员工');
    when others then
      dbms_output.put_line('其他例外');
  end;

假如抛出例外,没有执行到close cemp代码,也没问题,oracle的pmon进程(proccesss monitor)会回收进程资源

posted @ 2018-07-31 16:38  风雪夜_归人  阅读(93)  评论(0)    收藏  举报