繁体   English   中英

没有在预期的地方引发PL / SQL VALUE_ERROR异常

[英]PL/SQL VALUE_ERROR exception not raised where expected

我确信我的问题有一个简单的理论答案,但我找不到。

我有一个接受数字作为参数的程序。 它还具有VALUE_ERROR和OTHERS例外:

create or replace procedure test( p1 number) is
begin
    null;
exception
    when VALUE_ERROR then
        RAISE_APPLICATION_ERROR(-20001, 'value_error');
    when others then
        RAISE_APPLICATION_ERROR(-20000, 'others');
end;

我正在使用VARCHAR2参数执行该过程:

execute test('a');

...我希望显示的错误消息是

ORA-20001 value_error

但是,不幸的是,我得到了:

错误报告-ORA-06502:PL / SQL:数字或值错误:字符到数字的转换错误ORA-06512:在第1行06502。00000-“ PL / SQL:数字或值错误%s” *原因:算术运算,发生数字,字符串,转换或约束错误。 例如,如果尝试将值NULL分配给声明为NOT NULL的变量,或者试图将大于99的整数分配给声明的NUMBER(2),则会发生此错误。 *操作:更改数据,如何操作或声明数据,以使值不违反约束。

谁能解释这个问题,或者共享一个解释我为什么没有收到预期错误消息的链接?

非常感谢你,

正如Nicholas提到的那样,您不会收到消息,因为该异常不是在过程内部而是在执行之前引发的。

让我们看一个例子:

create or replace procedure test( p1 number) is
begin
    null;
exception
    when VALUE_ERROR then
        RAISE_APPLICATION_ERROR(-20001, 'PROC value_error');
    when others then
        RAISE_APPLICATION_ERROR(-20000, 'PROC others');
end;
/
begin
  test('a');
exception
    when VALUE_ERROR then
        RAISE_APPLICATION_ERROR(-20001, 'OUT value_error');
    when others then
        RAISE_APPLICATION_ERROR(-20000, 'OUT others');
end;

这里发生的是,您正在调用一个需要数字作为参数的过程,因此Oracle在执行匿名块期间尝试进行转换。 您看不到该过程中的消息,因为在进入该过程之前,将引发转换异常。

现在让我们看看如果更改过程会发生什么:

create or replace procedure test( p1 varchar2) is
param number;
begin
    param := p1;
exception
    when VALUE_ERROR then
        RAISE_APPLICATION_ERROR(-20001, 'PROC value_error');
    when others then
        RAISE_APPLICATION_ERROR(-20000, 'PROC others');
end;
/
begin
  test('a');
exception
    when VALUE_ERROR then
        RAISE_APPLICATION_ERROR(-20001, 'OUT value_error');
    when others then
        RAISE_APPLICATION_ERROR(-20000, 'OUT others');
end;

要不就:

begin
  test('a');
end;

查看该过程中引发的错误。

现在,该过程需要在其体内进行编号。 当执行到达该点时,它将从过程本身内部引发转换错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM