PL/SQL之例外
来源:互联网 发布:支付宝找回淘宝密码 编辑:程序博客网 时间:2024/06/05 22:34
1.例外的概念
在oracle中错误被叫做例外:分为系统例外和自定义例外。
2.系统例外
No_data_found(没有找到数据)、Too_many_rows(select ... into 语句匹配多个行)、Zero_Divide(被零除)、Value_error(算数或转换错误)、Timeout_on_resource(在等待资源时发生超时)
3. No_data_found
eg:
set serveroutput on
declare
pename emp.ename%type;
begin
select ename into pename from empwhere empno=1234;
exception
when no_data_found thendbms_output.put_line('没有找到该员工');
when others thendbms_output.put_line('其他例外');
end;
/
4.Too_many_rows
eg:
set serveroutput on
declare
pename emp.ename%type;
begin
select ename into pename from empwhere deptno=10;
exception
when too_many_rows thendbms_output.put_line('select into匹配了多个行');
when others thendbms_output.put_line('其他例外');
end;
/
5.Zero_Divide
eg:
set serveroutput on
declare
pnum number;
begin
pnum := 1/0;
exception
when zero_divide thendbms_output.put_line('1:0不能做被除数');
dbms_output.put_line('2:0不能做被除数');
when others thendbms_output.put_line('其他例外');
end;
/
Tips:then相当于大括号,后边可以有多个语句。
6.Value_error
eg:
set serveroutput on
declare
pnum number;
begin
pnum := 'abc';
exception
when value_error thendbms_output.put_line('算数或者转换错误');
when others thendbms_output.put_line('其他错误');
end;
/
7.自定义例外
定义变量,类型是exception
使用raise抛出自定义例外
eg:
set serveroutput on
declare
cursor cemp is select ename fromemp 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 thendbms_output.put_line('没有找到员工');
when others thendbms_output.put_line('其他例外');
end;
/
Tips: 此例子光标由于抛出异常没有正确关闭,但是oracle有一种机制,启动一个进程pmon(process monitor)关闭光标,释放资源和内存垃圾。
- PL/SQL之例外
- PL/SQL程序之例外
- PL/SQL常用例外
- PL/SQL处理例外
- PL/SQL exception(例外)
- PL/SQl自定义例外
- PL/SQL例外概念和系统例外
- PL/SQL预定义例外
- pl/sql进阶---例外处理
- oracle pl sql 抛出例外
- PL/SQL的No_data_found例外
- PL/SQL的value_error例外
- PL/SQL的too_many_rows例外
- PL/SQL的zero_divide例外
- pl/sq之例外处理
- pl/sq之例外处理
- PL/SQL编程整理4 -- 例外
- oracle pl/sql 例外(exception) 异常
- Android 笔记:android自定义View之自定义EditText(添加删除功能)
- php 微信开发之 微信支付 v3 配置
- jdk1.8下找不到javac
- openstack dashboard界面pause和suspend的区别
- 生成随机8位密码2位字母
- PL/SQL之例外
- IP网际协议
- 设计模式和常用算法概念
- DES加密算法原理
- selector和shape结合使用
- redis错误排查
- 一次inodes占用过多问题的解决
- 安装mysql5.7.16, 操作系统为Centos7
- 101个MySQL开源数据库调试和优化技巧