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. 预防 Session 劫持与 Session 固定攻击

    一.预防 Session 劫持 要求: ① 只允许通过 Cookie 来传递 SessionID ② 生成一个由 URL 传递的唯一标识作为 Session 的标记(token) 当请求同时包含有效的 ...

  2. CentOS通过日志反查入侵

    使用 last -f /var/log/wtmp 来查看可疑ip登录 查看/var/log/secure保存的系统信息 来确定可以ip登录

  3. 1CSS与文档

    层叠样式表(Cascading Style Sheet,CSS)的功能十分强大,可以影响一个或一组文档的表现. 为什么结构化对HTML来说很重要:1.非结构化页面使得建立内容索引极为困难.2.缺乏结构 ...

  4. JS中的事件冒泡(Bubble)和事件捕获(capture)以及如何阻止事件的冒泡

    对“捕获”和“冒泡”这两个概念,通常我们对冒泡了解和使用的会更多一些,因为在我们使用的所有浏览器中,都支持事件冒泡 ,即事件由子元素向祖先元素传播的,就 像气泡从水底向水面上浮一样.而在像firefo ...

  5. jquery on()方法绑定多个选择器,多个事件

    on(events,[selector],[data],fn) •events:一个或多个用空格分隔的事件类型和可选的命名空间,如"click"或"keydown.myP ...

  6. [转]android:clipToPadding和android:clipChildren

    转自:http://www.tuicool.com/articles/32YRJrq android:clipToPadding和android:clipChildren 时间 2014-10-14 ...

  7. 下拉列表select显示ng-options

    js中如何处理: it-equipment-list-maintenance-create-controller.js 'use strict'; myApp.controller( 'itEquip ...

  8. JS错误捕获

    try/catch/finally错误捕获 try { //一旦try中出现错误,直接跳到执行catch的内容,执行完catch的内容,代码继续执行 throw new Error('错误'); // ...

  9. 关键字static/const的作用

    static关键字的作用:(1)设置变量的存储域,函数体内static变量的作用范围为该函数体,不同于auto变量,该变量的内存只被分配一次,因此其值在下次调用时仍维持上次的值:(2)限制变量的作用域 ...

  10. CAS无锁算法与ConcurrentLinkedQueue

    CAS:Compare and Swap 比较并交换 java.util.concurrent包完全建立在CAS之上的,没有CAS就没有并发包.并发包借助了CAS无锁算法实现了区别于synchroni ...