为什么匿名PL / SQL块中没有立即引发异常?

时间:2015-10-17 20:42:39

标签: sql oracle exception plsql exception-handling

在下面的代码中,当主要数量的输入小于零或负数时,它应该引发异常并退出。但是下面执行块中的所有语句,最后显示异常块中写入的错误消息。

我希望Oracle在引发异常后退出。请帮助我解决这个问题。

declare
    principle1 number;
    rate1 number;
    r1 number;
    years1 number;
    interest1 number;
    principle2 number;
    rate2 number;
    r2 number;
    years2 number;
    interest2 number;
    invalid_entry exception;
begin

    principle1 := &principle1_amount1;

    if (principle1 <= 0) then
      raise invalid_entry;
    end if;

    rate1 := &rate_interest1;
    r1 := rate1/100;
    years1 := &years1;
    interest1 := ComptInt_jk(principle1,r1,years1);

    dbms_output.put_line('The interest for the principle amount ' || principle1
                         || ' for ' || years1 || ' year/s at the rate of ' || rate1
                         ||'% is '||interest1);

    principle2 := &principle1_amount2;
    rate2 := &rate_interest1;
    r2 := rate2/100;
    years2 := &years2;
    interest2 := ComptInt_jk(principle2,r2,years2);

    dbms_output.put_line('The interest for the principle amount ' || principle2
                         || ' for ' || years2 || ' year/s at the rate of ' || rate2
                         ||'% is '||interest2);
exception
    when invalid_entry then
       dbms_output.put_line('The data entered cannot be zero or negative ');
end;
/

1 个答案:

答案 0 :(得分:4)

  

&#34;当主要数量的输入小于零或-ve时   应该引发异常并退出,但以下执行所有操作   块中和末尾的语句显示错误消息   写在例外块&#34;

嗯,不太好。会发生什么是SQL * Plus要求我们输入 all 输入参数的值。您的代码需要五个输入参数,因此它会显示五个提示。

一旦我们输入了所有需求参数,程序就会评估if (principle1<=0 ) then,如果它是否定的,则会引发异常。控制流然后移动到异常块。

不显示其他任何消息。所以实际的代码没有被执行。看看:

.... 
 38* end;
Enter value for principle1_amount1: -89
old  16:     principle1:=&principle1_amount1;
new  16:     principle1:=-89;
Enter value for rate_interest1: 12
old  20:     rate1:=&rate_interest1;
new  20:     rate1:=12;
Enter value for years1: 12
old  22:     years1:=&years1;
new  22:     years1:=12;
Enter value for principle1_amount2: 12
old  25:     principle2:=&principle1_amount2;
new  25:     principle2:=12;
Enter value for rate_interest1: 12
old  26:     rate2:=&rate_interest1;
new  26:     rate2:=12;
Enter value for years2: 12
old  28:     years2:=&years2;
new  28:     years2:=12;
The data entered cannot be zero or negative

PL/SQL procedure successfully completed.

SQL>