SQL> -- 例外 exception
SQL> -- 1/0
SQL> ed
已写入 file afiedt.buf

1  declare
  2   pnum number := 0;
  3  begin
  4   pnum := 1/pnum; -- 会跑出一个叫zero_divide的异常
  5  exception
  6   when zero_divide then dmms_output.put_line('不能被0除');
  7* end;
  8  ed
  9  /
ed
 *
第 8 行出现错误:
ORA-06550: 第 8 行, 第 1 列:
PLS-00103: 出现符号 "ED"符号 "ED" 被忽略。

SQL> ed
已写入 file afiedt.buf

1  declare
  2   pnum number := 0;
  3  begin
  4   pnum := 1/pnum; -- 会跑出一个叫zero_divide的异常
  5  exception
  6   when zero_divide then dbms_output.put_line('不能被0除');
  7* end;
SQL> /
不能被0除

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

1  declare
  2   pnum number := 0;
  3  begin
  4   pnum := 1/pnum; -- 会跑出一个叫zero_divide的异常
  5* end;
  6  /
declare
*
第 1 行出现错误:
ORA-01476: 除数为 0
ORA-06512: 在 line 4

SQL> ed
已写入 file afiedt.buf

1  declare
  2   pnum number := 0;
  3  begin
  4   pnum := 1/pnum; -- 会跑出一个叫zero_divide的异常
  5  exception
  6   when zero_divide then dbms_output.put_line('不能被0除')
  7* end;
SQL> /
end;
   *
第 7 行出现错误:
ORA-06550: 第 7 行, 第 1 列:
PLS-00103: 出现符号 "END"在需要下列之一时:
:= . ( % ;
符号 ";" 被替换为 "END" 后继续。

SQL> ed
已写入 file afiedt.buf

1  declare
  2   pnum number := 0;
  3  begin
  4   pnum := 1/pnum; -- 会跑出一个叫zero_divide的异常
  5  exception
  6   when zero_divide then dbms_output.put_line('不能被0除');
  7* end;
SQL> /
不能被0除

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

1  declare
  2   pnum number := 0;
  3   pname emp.ename%type;
  4  begin
  5   select ename into pname from emp;
  6   pnum := 1/pnum; -- 会跑出一个叫zero_divide的异常
  7  exception
  8   when zero_divide then dbms_output.put_line('不能被0除');
  9* end;
SQL> /
declare
*
第 1 行出现错误:
ORA-01422: 实际返回的行数超出请求的行数
ORA-06512: 在 line 5

SQL> ed
已写入 file afiedt.buf

1  declare
  2   pnum number := 0;
  3   pname emp.ename%type;
  4  begin
  5   select ename into pname from emp;
  6   pnum := 1/pnum; -- 会跑出一个叫zero_divide的异常
  7  exception
  8   when zero_divide then dbms_output.put_line('不能被0除');
  9   when too_many_rows then dbms_output.put_line('太多的行');
 10* end;
SQL> /
太多的行

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

1  declare
  2   pnum number := 0;
  3   pname emp.ename%type;
  4  begin
  5   select ename into pname from emp;
  6   pnum := 1/pnum; -- 会跑出一个叫zero_divide的异常
  7  exception
  8   when zero_divide then dbms_output.put_line('不能被0除');
  9   when too_many_rows then dbms_output.put_line('太多的行');
 10   when others then dbms_output.put_line('qita');
 11* end;
SQL> /
太多的行

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

1  declare
  2   pnum number := 0;
  3   pname emp.ename%type;
  4  begin
  5   select ename into pname from emp;
  6   pnum := 1/pnum; -- 会跑出一个叫zero_divide的异常
  7  exception
  8   when zero_divide then dbms_output.put_line('不能被0除');
  9   when others then dbms_output.put_line('qita');
 10* end;
SQL> /
qita

PL/SQL 过程已成功完成。

SQL> -- 自定义列外
SQL> ed
已写入 file afiedt.buf

1  -- 查询50号部门的所有员工的姓名,如果没有查到数据跑出列外
  2  declare
  3   cursor cemp is select ename from emp where deptno = 50;
  4   pname emp.ename%type;
  5   -- 自己定义异常
  6   not_emp_data exception;
  7  begin
  8   open cursor;
  9        if cemp%notfound then raise not_emp_data;
 10    else
 11    loop
 12     fetch cemp into pname;
 13     exit when cemp%notfound;
 14     dbms_output.put_line(pname);
 15    end loop;
 16        end if;
 17   close cursor;
 18  exception
 19   when not_emp_data then dbms_output.put_line('没有找到50号部门的数据');
 20* end;
 21  /
      if cemp%notfound then raise not_emp_data;
                       *
第 9 行出现错误:
ORA-06550: 第 8 行, 第 7 列:
PLS-00201: 必须声明标识符 'CURSOR'
ORA-06550: 第 8 行, 第 2 列:
PL/SQL: SQL Statement ignored
ORA-06550: 第 17 行, 第 8 列:
PLS-00201: 必须声明标识符 'CURSOR'
ORA-06550: 第 17 行, 第 2 列:
PL/SQL: SQL Statement ignored

SQL> ed
已写入 file afiedt.buf

1  -- 查询50号部门的所有员工的姓名,如果没有查到数据跑出列外
  2  declare
  3   cursor cemp is select ename from emp where deptno = 50;
  4   pname emp.ename%type;
  5   -- 自己定义异常
  6   not_emp_data exception;
  7  begin
  8   open cemp;
  9        if cemp%notfound then raise not_emp_data;
 10    else
 11    loop
 12     fetch cemp into pname;
 13     exit when cemp%notfound;
 14     dbms_output.put_line(pname);
 15    end loop;
 16        end if;
 17   close cursor;
 18  exception
 19   when not_emp_data then dbms_output.put_line('没有找到50号部门的数据');
 20* end;
SQL> ed
已写入 file afiedt.buf

1  -- 查询50号部门的所有员工的姓名,如果没有查到数据跑出列外
  2  declare
  3   cursor cemp is select ename from emp where deptno = 50;
  4   pname emp.ename%type;
  5   -- 自己定义异常
  6   not_emp_data exception;
  7  begin
  8   open cemp;
  9        if cemp%notfound then raise not_emp_data;
 10    else
 11    loop
 12     fetch cemp into pname;
 13     exit when cemp%notfound;
 14     dbms_output.put_line(pname);
 15    end loop;
 16        end if;
 17   close cemp;
 18  exception
 19   when not_emp_data then dbms_output.put_line('没有找到50号部门的数据');
 20* end;
SQL> /

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

1  -- 查询50号部门的所有员工的姓名,如果没有查到数据跑出列外
  2  set serveroutput on
  3  declare
  4   cursor cemp is select ename from emp where deptno = 50;
  5   pname emp.ename%type;
  6   -- 自己定义异常
  7   not_emp_data exception;
  8  begin
  9   open cemp;
 10        if cemp%notfound then raise not_emp_data;
 11    else
 12    loop
 13     fetch cemp into pname;
 14     exit when cemp%notfound;
 15     dbms_output.put_line(pname);
 16    end loop;
 17        end if;
 18   close cemp;
 19  exception
 20   when not_emp_data then dbms_output.put_line('没有找到50号部门的数据');
 21* end;
SQL> /
 cursor cemp is select ename from emp where deptno = 50;
 *
第 4 行出现错误:
ORA-00922: 选项缺失或无效

SQL> ed
已写入 file afiedt.buf

1  -- 查询50号部门的所有员工的姓名,如果没有查到数据跑出列外
  2  declare
  3   cursor cemp is select ename from emp where deptno = 50;
  4   pname emp.ename%type;
  5   -- 自己定义异常
  6   not_emp_data exception;
  7  begin
  8   open cemp;
  9        if cemp%notfound then raise not_emp_data;
 10    else
 11    loop
 12     fetch cemp into pname;
 13     exit when cemp%notfound;
 14     dbms_output.put_line(pname);
 15    end loop;
 16        end if;
 17   close cemp;
 18  exception
 19   when not_emp_data then dbms_output.put_line('没有找到50号部门的数据');
 20* end;
SQL> set serveroutput on;
SQL> -- 查询50号部门的所有员工的姓名,如果没有查到数据跑出列外
SQL> declare
  2   cursor cemp is select ename from emp where deptno = 50;
  3   pname emp.ename%type;
  4   -- 自己定义异常
  5   not_emp_data exception;
  6  begin
  7   open cemp;
  8        if cemp%notfound then raise not_emp_data;
  9    else
 10    loop
 11     fetch cemp into pname;
 12     exit when cemp%notfound;
 13     dbms_output.put_line(pname);
 14    end loop;
 15        end if;
 16   close cemp;
 17  exception
 18   when not_emp_data then dbms_output.put_line('没有找到50号部门的数据');
 19  end;
 20  /

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

1  declare
  2   cursor cemp is select ename from emp where deptno = 50;
  3   pname emp.ename%type;
  4   -- 自己定义异常
  5   not_emp_data exception;
  6  begin
  7   open cemp;
  8        if cemp%notfound then
  9    dbms_output.put_line('11');
 10    raise not_emp_data;
 11    else
 12    loop
 13     fetch cemp into pname;
 14     exit when cemp%notfound;
 15     dbms_output.put_line(pname);
 16    end loop;
 17        end if;
 18   close cemp;
 19  exception
 20   when not_emp_data then dbms_output.put_line('没有找到50号部门的数据');
 21* end;
SQL> /

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

1  declare
  2   cursor cemp is select ename from emp where deptno = 50;
  3   pname emp.ename%type;
  4   -- 自己定义异常
  5   not_emp_data exception;
  6  begin
  7   open cemp;
  8   fetch cemp into pname;
  9        if cemp%notfound then
 10    dbms_output.put_line('11');
 11    raise not_emp_data;
 12    else
 13    loop
 14     exit when cemp%notfound;
 15     dbms_output.put_line(pname);
 16    end loop;
 17        end if;
 18   close cemp;
 19  exception
 20   when not_emp_data then dbms_output.put_line('没有找到50号部门的数据');
 21* end;
SQL> /
11                                                                                                                                                                                                     
没有找到50号部门的数据

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

1  declare
  2   cursor cemp is select ename from emp where deptno = 50;
  3   pname emp.ename%type;
  4   -- 自己定义异常
  5   not_emp_data exception;
  6  begin
  7   open cemp;
  8   fetch cemp into pname;
  9        if cemp%notfound then
 10    dbms_output.put_line('11');
 11    raise not_emp_data;
 12    else
 13    loop
 14     exit when cemp%notfound;
 15     dbms_output.put_line(pname);
 16    end loop;
 17        end if;
 18   close cemp;
 19  exception
 20   when not_emp_data then dbms_output.put_line('没有找到50号部门的数据');
 21* end;
SQL> /
11                                                                                                                                                                                                     
没有找到50号部门的数据

PL/SQL 过程已成功完成。

SQL> select sum(sal) from emp;

SUM(SAL)                                                                                                                                                                                             
----------                                                                                                                                                                                             
     47025

SQL> spool off;

exception的更多相关文章

  1. [C#] C# 知识回顾 - 你真的懂异常(Exception)吗?

    你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...

  2. 浅谈java异常[Exception]

    学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:589809992 我们一起学Java! 一. 异常的定义 在<java编程思想 ...

  3. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

    学习架构探险,从零开始写Java Web框架时,在学习到springAOP时遇到一个异常: "C:\Program Files\Java\jdk1.7.0_40\bin\java" ...

  4. Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V

    在学习CGlib动态代理时,遇到如下错误: Exception in thread "main" java.lang.NoSuchMethodError: org.objectwe ...

  5. Atitit 解决Unhandled event loop exception错误的办法

    Atitit 解决Unhandled event loop exception错误的办法 查看workspace/.metadata/.log org.eclipse.swt.SWTError: No ...

  6. Java中的Checked Exception——美丽世界中潜藏的恶魔?

    在使用Java编写应用的时候,我们常常需要通过第三方类库来帮助我们完成所需要的功能.有时候这些类库所提供的很多API都通过throws声明了它们所可能抛出的异常.但是在查看这些API的文档时,我们却没 ...

  7. Error on line -1 of document : Premature end of file. Nested exception: Premature end of file.

    启动tomcat, 出现, ( 之前都是好好的... ) [lk ] ERROR [08-12 15:10:02] [main] org.springframework.web.context.Con ...

  8. Android开发学习之路-关于Exception

    Exception在Java中是表示异常的一个类.它是Throwable的子类. 而Exception的子类RuntimeException是一个特殊的异常类,在代码中不需要对此类进行throw,而是 ...

  9. 严重: Exception sending context initialized event to listener instance of class

    问题描述:Exception sending context initialized event to listener instance of class org.springframework.w ...

  10. python之最强王者(11)——异常(exception)

    1.Python 异常处理 python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 异常处理: 本站Python教程会具体介绍. ...

随机推荐

  1. thinkphp3.2 cli模式的正确使用方法

    最近要使用thinkphp3.2版本的cli模式,手动执的话没有问题,比如php /www/index.php home/article/get 这样没有问题,但是一般用cli模式都是定时任务比较多, ...

  2. MEMORY Storage Engine MEMORY Tables TEMPORARY TABLE max_heap_table_size

    http://dev.mysql.com/doc/refman/5.7/en/create-table.html You can use the TEMPORARY keyword when crea ...

  3. 转载:GridControl使用技巧

    一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 (1).gridView.AddN ...

  4. 一个支持FMX.Win框架的托盘控件

    不多说了 直接上代码........有任何问题请给我邮件.... // **************************************************************** ...

  5. python时间操作总结

    Unix时间戳 Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月 ...

  6. MySQL基础CRUD编程练习题的自我提升(1)

    基础知识: 1.数据库的连接 mysql -u -p -h -u 用户名 -p 密码 -h host主机 2:库级知识 2.1 显示数据库: show databases; 2.2 选择数据库: us ...

  7. [Android Tips] 14. Using Proguard with Android without obfuscation

    Option -dontobfuscate REF Using Proguard with Android without obfuscation

  8. C# xml压缩包不解压的情况下解析xml内容

    string sourceFilePath = @"E:\文件拷贝\xx\3773\3773.zip"; FileInfo fileInfo = new FileInfo(sour ...

  9. PHP读取XML文件数据

    XML文件 <?xml version="1.0" encoding="UTF-8"?> <node> <student> ...

  10. JQuery-Ajax后台提交数据与获取数据

    function jqajax(){ var urlName = $("#urlName").val(); var urla = $("#url").val() ...