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. ThinkPHP 3.2.3 简单后台模块开发(二)RBAC

    RBAC(Role-Based Access Controll)基于角色的访问控制 在 ThinkPHP3.2.3 中 RBAC 类位于 /ThinkPHP/Library/Org/Util/Rbac ...

  2. java IO 学习总结

    推荐文章:java I/O学习 只要是处理纯文本数据,就要优先考虑使用字符流,除此之外都用字节流 字符流:FileReader和BufferedReader的使用 String path = &quo ...

  3. git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origin

    git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origi ...

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

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

  5. http://www.cnblogs.com/softidea/p/5631763.html

    http://www.cnblogs.com/softidea/p/5631763.html

  6. LeetCode Power of Three

    原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...

  7. URLRedirector 解决网页上无法访问 google CDN 的问题(fonts、ajax、themes、apis等)

    URLRedirector 解决网页上无法访问 google CDN 的问题(fonts.ajax.themes.apis等) 由于某些原因,在访问国外的网站时有时候会特别慢,像 stackoverf ...

  8. ELK+kafka构建日志收集系统

    ELK+kafka构建日志收集系统   原文  http://lx.wxqrcode.com/index.php/post/101.html   背景: 最近线上上了ELK,但是只用了一台Redis在 ...

  9. 如何学习Python

    [整理]如何学习Python + 如何有效利用Python有关的网络资源 + 如何利用Python自带手册(Python Manual) http://www.crifan.com/howto_lea ...

  10. Webstorm的序列号和证书

    User Name: ------------name-------------- EMBRACE -------------name-------------- License Key: ===== ...