Oracle:PLSQL——例外

例外,相当于java中的异常,是程序设计语言提供的一种功能,用来增强程序的健壮性和容错性。

一、系统内置例外:
例外
解释
no_date_found 没有找到数据
too_many_rows select...into语句匹配多行
zero_Divide 被零除
value_error 算术或转行错误
timeout_on_resource 在等待资源时发生超时
例1:使用oracle系统内置例外,演示除0例外【zero_divide】
declare
    myresult number;
begin
    myresult := 1/0;
    dbms_output.put_line(myresult);
exception
    when zero_divide then
  dbms_output.put_line('除数不能为0');
  delete from emp; 
end;
/
例2:使用oracle系统内置例外,查询100号部门的员工姓名,演示没有找到数据【no_data_found】
declare
    pename varchar2(20);
begin
    select ename into pename from emp where deptno = 100;
    dbms_output.put_line(pename);
exception
    when NO_DATA_FOUND then
  dbms_output.put_line('查无该部门员工');
  insert into emp(empno,ename) values(1111,'ERROR');
end;
/
二、自定义例外:
1、 declare 节中 定义 例外  
     out_of  exception ;
2、 begin 节中可行语句中 抛出 例外 
      raise out_of ;
3、在 exception 处理 例外
     when out_of then …

猜你喜欢

转载自blog.csdn.net/weixin_41113108/article/details/80348710