Home » SQL & PL/SQL » SQL & PL/SQL » How to remove when others then (Oracle 10g)
How to remove when others then [message #596115] Wed, 18 September 2013 02:22 Go to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Hi All,

Please help me to remove when others then exception handling from the following code.I want to check if any of the variable is null then it should raise an exception and insert into the log table.But how can i change it.
declare
l_dummy varchar2(50):='value';
l_dummy1 varchar2(50);
l_str varchar2(100):='error occured in ';
l_msg varchar2(100);
begin
    begin
    if l_dummy is null  then 
    l_msg:=l_msg||'l_dummy';
    end if;
    if l_dummy1 is null then 
    l_msg:=l_msg||' l_dummy1';
    end if;
    
    if l_msg is not null then
    raise_application_error(-20000,'error occured');
    end if;
    exception
    when others then 
    dbms_output.put_line(l_str ||l_msg);
   --insert into log table
    --raise;
    end;
end;


Regards,
Nathan
Re: How to remove when others then [message #596116 is a reply to message #596115] Wed, 18 September 2013 02:44 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
If a variable (in your code) is null, it is not an "exception". All you need to say is
if l_dummy is null then
   insert into log_table ...
end if;
Re: How to remove when others then [message #596118 is a reply to message #596116] Wed, 18 September 2013 02:48 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thanks LittleFoot,

It is not an exception but what if I want to stop the execution itself when it happens then .

[Updated on: Wed, 18 September 2013 02:49]

Report message to a moderator

icon14.gif  Re: How to remove when others then [message #596119 is a reply to message #596116] Wed, 18 September 2013 02:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
... and note that logging should be made in a procedure itself, maybe with "autonomous_transaction" pragma (depending on what you want to log).

In addition, if you need to raise an exception in your code you want to trap elsewhere there is no need to trap ALL exceptions (WHEN OTHERS):
declare
  x exception;
begin
  ...
  if <some condition> then raise x; end if;
  ...
exception 
  when x then <do something>;
end;
/


[Updated on: Wed, 18 September 2013 02:52]

Report message to a moderator

Re: How to remove when others then [message #596124 is a reply to message #596119] Wed, 18 September 2013 03:22 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thank you very much Michel I got it really.
Re: How to remove when others then [message #596125 is a reply to message #596124] Wed, 18 September 2013 03:29 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Though if you want an error message go back to the caller it's probably as easy to write:
declare
l_dummy varchar2(50):='value';
l_dummy1 varchar2(50);
l_str varchar2(100):='error occured in ';
l_msg varchar2(100);
begin
    begin
    if l_dummy is null  then 
    l_msg:=l_msg||'l_dummy';
    end if;
    if l_dummy1 is null then 
    l_msg:=l_msg||' l_dummy1';
    end if;
    
    if l_msg is not null THEN
      <do whatever, including calling logging procedure>
      raise_application_error(-20000,'error occured');
    end if;

end;

Re: How to remove when others then [message #596128 is a reply to message #596125] Wed, 18 September 2013 03:46 Go to previous message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Yes CookieMonster this is also good point you made. I will log before raising the error but like Michel's way for avoiding when others then exception.

Thank you all guys.
Previous Topic: Trigger for updating all columns of a table
Next Topic: In clause not working
Goto Forum:
  


Current Time: Wed Apr 24 22:40:23 CDT 2024