2016-12-25 13 views
0

私の質問には単純な理論的な答えがありますが、それを見つけることはできません。PL/SQLのVALUE_ERROR例外が発生しませんでした。

私はパラメータとしてNUMBERを受け取る手続きを持っています。

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のPARAMTERで手順を実行しています:それはまたVALUE_ERRORとOTHERS例外持っている私は、エラーメッセージがあることを表示することを期待して...

execute test('a'); 

ORA-20001 value_error

しかし、残念ながら、私が得た:

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. 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). *Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.

誰でもこれを説明することができますか、または、なぜ私が予想されるエラーメッセージを受け取らないのか説明されているリンクを共有できますか?ニコラスは、例外がないプロシージャ内ではなく、それを実行する前にスローされますので、あなたがあなたのメッセージを得ることはありません述べたように

は、非常に多くの

+4

'exception'セクションには決して行きません。数字が期待されるときにプロシージャをコールすると、PL/SQLエンジンはその文字を暗黙的に数値に変換しようとしますが、そうでない場合は例外が発生します。プロシージャは決して実行されません。その例外をトラップする場合は、別のPL/SQLブロック内のプロシージャをコールします。 –

+0

暗黙的な変換を実行できないために例外が発生するため、なぜVALUE_ERRORを発生させないのですか。これは事前定義されていますか? – mikcutu

+1

'VALUE_ERROR' =' ORA-06502 PL/SQL:数値または値のエラー '他に何が見えますか? –

答えて

4

、ありがとうございました。

例を見てみましょう:ここに何が起こる

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; 

エラーが手順でスロー見に。

この手順では、本体内にnumberが必要です。実行がそのポイントに達すると、プロシージャ自体から変換エラーがスローされます。

関連する問題