简体   繁体   English

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

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

I am sure my question has a simple theoretical answer but I cannot find it. 我确信我的问题有一个简单的理论答案,但我找不到。

I have a procedure which accepts as parameter a NUMBER. 我有一个接受数字作为参数的程序。 It also have the VALUE_ERROR and OTHERS exceptions: 它还具有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;

I am executing the procedure with a VARCHAR2 paramter: 我正在使用VARCHAR2参数执行该过程:

execute test('a');

... an I expect that the error message displayed to be ...我希望显示的错误消息是

ORA-20001 value_error ORA-20001 value_error

but, unfortunately, I got: 但是,不幸的是,我得到了:

Error report - ORA-06502: PL/SQL: numeric or value error: character to number conversion error ORA-06512: at line 1 06502. 00000 - "PL/SQL: numeric or value error%s" *Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. 错误报告-ORA-06502:PL / SQL:数字或值错误:字符到数字的转换错误ORA-06512:在第1行06502。00000-“ PL / SQL:数字或值错误%s” *原因:算术运算,发生数字,字符串,转换或约束错误。 For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2). 例如,如果尝试将值NULL分配给声明为NOT NULL的变量,或者试图将大于99的整数分配给声明的NUMBER(2),则会发生此错误。 *Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints. *操作:更改数据,如何操作或声明数据,以使值不违反约束。

Can anyone explain this, or share a link where it is explained why I do not receive expected error message? 谁能解释这个问题,或者共享一个解释我为什么没有收到预期错误消息的链接?

Thank you very much, 非常感谢你,

As Nicholas mentioned you don't get your message because the exception is thrown not inside the procedure but before executing it. 正如Nicholas提到的那样,您不会收到消息,因为该异常不是在过程内部而是在执行之前引发的。

Let's look at an example: 让我们看一个例子:

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;

What happens here is that you're calling a procedure that requires number as parameter so Oracle tries conversion during execution of anonymous block. 这里发生的是,您正在调用一个需要数字作为参数的过程,因此Oracle在执行匿名块期间尝试进行转换。 You can't see the message from the procedure because before entering the procedure the conversion exception is thrown. 您看不到该过程中的消息,因为在进入该过程之前,将引发转换异常。

Now let's see what happens if we change the procedure: 现在让我们看看如果更改过程会发生什么:

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;

Or just: 要不就:

begin
  test('a');
end;

to see the error thrown in the procedure. 查看该过程中引发的错误。

Now the procedure requires number within its body; 现在,该过程需要在其体内进行编号。 when execution reaches that point it throws the conversion error, from within the procedure itself. 当执行到达该点时,它将从过程本身内部引发转换错误。

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

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