Exception in a Trigger???

Hi Guru's,
Can we handle exceptions in Triggers?
Thanks
Edited by: user10679113 on Dec 31, 2008 1:29 PM

Sure you can. Imp point to take note of
# If you use a LOGON trigger to monitor logons by users, include an exception-handling part in the trigger, and include a WHEN OTHERS exception in the exception-handling part. Otherwise, an unhandled exception might block all connections to the database.
# If you use a LOGON trigger only to execute a package (for example, an application context-setting package), put the exception-handling part in the package instead of in the trigger.

Similar Messages

  • Exception in Oracle Trigger in Java

    I have a java trigger loaded in my Oracle database. In the trigger, I throw an exception and, when the trigger is run, the exception is caught by the JVM and the following error occurs:
    ORA-29532: Java call terminated by uncaught Java exception: br.com.medisoft.sga.trigger.exception.MedisoftTriggerException: Mensagem DBA-1
    The SQL statement is executed in an EJB using JDBC. Is there any method to catch the exception thrown by the java trigger (in the example, MedisoftTriggerException) and it's methods?
    The MedisoftTriggerException is an subclass of SQLException.
    I tried the following:
    try {
    } catch ( SQLException ex ) {
    I can catch an SQLException, but with the following data:
    ex.getMessage () => ORA-29532: Java call terminated by uncaught Java exception: br.com.medisoft.sga.trigger.exception.MedisoftTriggerException: Mensagem DBA-1
    ex.errorCode () => 29532
    ex.getNextException () => null.
    I want to get the data from exception br.com.medisoft.sga.trigger.exception.MedisoftTriggerException, that was the really thrown exception in the trigger. Is there another way than to use the substring from ex.getMessage() ?
    Thanks,
    Jonatan Schroeder
    [email protected]

    Sorry, wrong forum.

  • Jdbc handling of no data found exception in a trigger

    Hi,
    I am using Oracle 11.1.0.6 oci jdbc driver.
    When an insert statement fails due to a before insert trigger throwing a ora-01403, the jdbc driver does not throw an exception. From what I can find on MetaLink its because its considered a warning. Now I know that in the past the thin driver did not work this way, it was considered an error.
    Does anyone know a good reference to look that explains how this is supposed to work as standard. Is what the Oracle oci driver doing in 11G standard?
    Thanks
    Pete

    Hi,
    I looked at the getWarnings method. But when I use this for the statement object it does not return anything. Even though Note 101528.1 says that this condition is treated as a warning.
    Pete

  • Enable exception handling in trigger

    Hello-
    I am fairly new to Oracle, and have written a simple audit trigger to insert old values into an audit table upon updates to an existing table. I am trying to incorporate some exception handling in case there are errors. Is there a way to 'enable' or call out the oracle predefined the errors, or do I have to specifically write conditions for all the exceptions I want to capture?
    Thanks!

    Hi
    There are two types of expections exists in oracle, i.e. internal & external exceptions. Internals are oracle defined and externals are user defined.
    Internal
    When no_data_found
    when too_many_rows
    when divide_zero (read oracle documentation for more)
    example:
    declare
    begin
    select ename,sal from emp where empno=0;
    exception
    when no_data_found then
    dbms_output.put_line('No such employee exists');
    end;
    external
    =======
    you define in the declaretive part
    example:
    declare
    MyException exception
    begin
    if .... then
    raise MyException;
    end if;
    exception
    when Myexception then
    do_action
    end;

  • Own exception  and to trigger

    Hi
    Is possible to declate a exception and automatically to trigger ?

    THE WAY what is utility PRAGMA
    EXCEPTION_INIT ?it like user defined exception
    somthing like this?SQL> ed
    Wrote file afiedt.buf
      1  DECLARE
      2     -- name for exception
      3       e_emps_remaining EXCEPTION;
      4     PRAGMA EXCEPTION_INIT(e_emps_remaining, -20002);
      5  BEGIN
      6     raise_application_error(-20002, 'remaining');
      7  EXCEPTION
      8     WHEN e_emps_remaining THEN
      9     DBMS_OUTPUT.PUT_LINE ('sql code error :'||sqlcode||' : '||sqlerrm);
    10* END;
    SQL> /
    sql code error :-20002 : ORA-20002: remaining
    PL/SQL procedure successfully completed.

  • Mutating table exception on trigger with After Insert but not with before

    Hi
    I need to maintain some constraint on the table to have only one row for values of the columns but not by primary key constraint.
    Because in case of primary key the insert would fail and the rest of the operation would be discontinued, I cannot change in the somponent that inserts the row so I have to prevent that on the table I have.
    I created a before insert trigger on the table which checks if any row exists in the table with same column values as the one being inserted. if found I delete the rows and let the insert happen (w/o raising any error). if the rows do not exist then the insert shall be continued.
    I read at place that modifying the dame table in the trigger body shall raise a mutating table exception, but I donot get the exception when the trigger is fired.
    Just when I change the trigger to after insert trigger then the nutating table exception is thrown.
    Is it the right behavior i.e. the Before insert trigger does not raise the exception and only after insert does that, since I could not find the example for before insert triggers throwing such exception so I think it is better to confirm it before finalizing the implementation.
    Thanks
    Sapan

    sapan wrote:
    Hi Tubby
    I cannot user unique constraint because that would raise an exception upon violation and the third party component that is inserting in the table would fail.
    That component does some other tasks as well after this insert and if an exception is raised then those tasks would not be performed.
    Also I cannot change the component to ignore this exception.Well then, you're in a bit of a pickle.
    I'm guessing the trigger you have been working on isn't "safe". By that i mean that it doesn't account for multi-user scenarios. You'll need to serialize access to the data elements in question and implement some sort of locking mechanism to ensure that only 1 session can work with those values.
    After you work out how to do that it sounds as though you would be better served using an INSTEAD OF trigger (you'd need to implement this on a view which is made off of your base table).
    Here's one way you can work on serializing access to your table on a relatively fine grained level (as opposed to locking the entire table).
    Re: possible to lock stored procedure so only one session may run it at a time?
    Cheers,

  • FRM 40735 Post Query trigger raised unhandled exception TOO_MANY_ROWS

    I'm using Developer 2000 Forms 4.5 and after a query on the form; an error message is on the screen "FRM 40735 Post Query trigger raised unhandled exception TOO_MANY_ROWS". And form can't be closed.
    How can I solve this problem?

    You must have a POST-QUERY trigger defined somewhere in your form.
    In this trigger you probably do a "select into" that retrieves more than one row.
    That explains the "too many rows" error.
    When you do a query in the form, the POST-QUERY trigger fires for each row retrieved.
    So if you do a query on a underlying table that has 100 rows, the POST-QUERY trigger fires 100 times. After 100 alerts, so to speak, you'll be able to close your form.
    Solution off course is to correct the POST-QUERY trigger by making sure you only retrieve one row with the select into, or define a too_many_rows exception in that trigger.
    Good luck.

  • Use of "DBA_OBJECTS" in "AFTER CREATE" Trigger

    Hi,
    We would like to store some extra information about our objects we have in the database. To do so we tought of the idea of creating a 'documenting' table containing an object_id that references to the object_id from the view dba_views.
    Now we want to automatically create a new record in our documenting table when a new object is created, in the first stage this should only contain the object_id
    To accomplish this we are using an 'AFTER CREATE' trigger, but when this trigger fires and the code searches for the new object_id in dba_objects, it does not return any records and generates an error. Likely because when the trigger is executed, the view is not yet updated?
    create or replace
    TRIGGER TRG_TEST
    AFTER CREATE ON SCOTT.SCHEMA
    DECLARE
    tmp VARCHAR2(50);
    BEGIN
    dbms_output.put_line(ora_dict_obj_name);
    select object_id
    into tmp
    from dba_objects
    where object_name = ora_dict_obj_name;
    dbms_output.put_line(tmp);
    END;
    Error report:
    ORA-04088: Fout bij uitvoering van trigger 'SCOTT.TRG_TEST'.
    ORA-01403: Geen gegevens gevonden.
    ORA-06512: in regel 6
    04088. 00000 - "error during execution of trigger '%s.%s'"
    *Cause:    A runtime error occurred during execution of a trigger.
    *Action:   Check the triggers which were involved in the operation.
    It's in dutch, so I'll have a go at translating:
    Error report:
    ORA-04088: Exception while executing trigger 'SCOTT.TRG_TEST'.
    ORA-01403: No data found
    ORA-06512: in rule 6
    04088. 00000 - "error during execution of trigger '%s.%s'"
    *Cause:    A runtime error occurred during execution of a trigger.
    *Action:   Check the triggers which were involved in the operation.
    Does anyone have an idea of how I can accomplish what I'm trying to do here.. Or maybe something I'm doing wrong?
    Thanks in advance!
    Davy

    What is "ora_dict_obj_name" defined as?
    Another option might be to setup a DBMS_SCHEDULER job to run the following insert
    firstly though, create the following table ;
    create document_table as (select * from dba_objects where rownum < 1)and then setup a DBMS_SCHEDULER job to run the following:
    begin
      insert into document_table
      (select dbo.*
       from   dba_objects    dbo
             ,document_Table dtb
       where  dbo.Object_ID = dtb.Object_ID(+)
       and    dtb.Object_ID is null);
       commit;
       exception
        when others then
          rollback;
          -- log a message somewhere with the error, i.e. SQLERRM
    end;
    /Note, I specified all columns, but you'll probably specify explicitly which column you require, in which case the CREATE table would change as well.
    Then you won't have a need for any trigger, and can better manage when you want your documentation to be updated.
    Another option might be to use COMMENT, which can be used on tables, views, materialized views, but won't cover all objects
    to the extent you might want.
    SQL> desc emp2
    Name                                                                                                  
    EMPNO                                                                                                 
    JOB                                                                                                   
    START_DATE                                                                                            
    SAL                                                                                                   
    DEPT                                                                                                  
    END_DATE                                                                                              
    SQL> comment on column emp2.sal is 'Existing Salary of employee as paid at most recent month end';

  • SOAP to FILE using BPM to catch exception and save error into the table

    Hi All
    My scenario SOAP to FILE using BPB is working fine but now I have a requirement to catch an exception if something wrong happened on a runtime and save it into the tracking table, is that possible, if it is please point me to the similar step by step scenario.
    Thanks in advince,
    Yonela

    Yonela:
    As your original requirement is to save the eorr data into your database table, however, you was mis-leaded to alerting field.
    You are using BPM now, then that is the reason that I suggested to use excpetion branch.
    It does not matter SOAP to File seneario, it does not have to be SAP - RFC scenario, your BPM willl interact with database system.
    First you have block which include all the steps that possible generate exception: like Transformation step, and your final Aync send step (which will send data to file).
    Secondly, define a exception handler for that block.
    3. Modify each steps that posssible generate exception: e.g. transformation step, send step, add exception handler to them.
    4. Create exception branch in side the block
    5. Add another send step inside your exception branch, which will call RFC, RFC will write data to your table.
    6. If you want, you can add a control step in exception branch to trigger a alert, or terminate current process.
    At run time, any steps that trigger exeption,will cause your exception branch executed, and RFC will be called to insert data into your database table.
    Regards
    Liang

  • Call a Procedure in Trigger

    Dear Sir
    How to call a Procedure in Trigger and if any exception occurs in trigger then How do we rollback it?
    Regards
    Thakur Manoj R

    [email protected] wrote:
    Dear Sir
    How to call a Procedure in Trigger Just call it by passing its parameters.
    and if any exception occurs in trigger then How do we rollback it?You dont have to rollback. Trigger depends on the parent transaction. So if you just allow the raise the exception it will automatically rollback.

  • WHEN-VALIDATE-TRIGGER trigger looping so many times

    Hi,
    Below is the code written in WHEN-VALIDATE-TRIGGER trigger and after raise FORM-TRIGGER_FAILURE
    the process is not stopping and the trigger message looping without end when
    I tab to the next field or try to save the record. I am not sure why the flow did not stop
    after the raise FORM_TRIGGER_FAILURE exception and the trigger looping again and again.
    Please help me to know what is wrong in the code. This trigger written in ITEM level.
    declare
                     msg_type varchar2(3);
                  msg_text varchar2(2000);
                  continue       boolean;
                begin
                  if :pom1_b22.qty < 0 then
                    message_ppkg.get_msg_from_db('RET','04060',msg_type,msg_text);
                    message(msg_text || '.');
                    bell;
                    raise form_trigger_failure;
                  else
                    continue := TRUE;
                    if :pom1_b22.qty is not null and :pom1_b22a.b22_query = 'FALSE' then
                      if nvl(:pom1_b22.irt_created,'N') = 'Y' then
                        if :pom1_b22.qty <> :pom1_b22.save_qty and
                          :pom1_b22.save_qty is not null then                    
                          if check_order_status then
                            continue := TRUE;
                          else
                            continue := FALSE;
                             message_ppkg.get_msg_from_db('RET','04061',msg_type,msg_text);
                             message(msg_text);   
                            bell;
                            :pom1_b22.qty := :pom1_b22.save_qty;
                            raise form_trigger_failure;
                          end if;
                        end if;
                      end if;                
                      if continue then
                        if :pom1_b22.save_qty is null then
                          if :pom1_b22.qty > :pom1_b22a.total_qty then
                             message_ppkg.get_msg_from_db('RET','04057',msg_type,msg_text);
                             message(msg_text || to_char(:pom1_b22a.total_qty) || '.');   
                            bell;
                            raise form_trigger_failure;
                          end if;
                        else
                          if :pom1_b22.qty <> nvl(:pom1_b22.save_qty,0) then
                            if :pom1_b22.qty > :pom1_b22a.total_qty +
                                               (nvl(:pom1_b22.save_qty,0) -
                                                :pom1_b22.qty) then
                                                    message_ppkg.get_msg_from_db('RET','04056',msg_type,msg_text);
                             message(msg_text || to_char(:pom1_b22a.total_qty +
                                              (nvl(:pom1_b22.save_qty,0) -
                                               :pom1_b22.qty)));                 
                              bell;
                              raise form_trigger_failure;
                            end if;
                          end if;
                        end if;
                      end if;
                    end if;
                  end if;
                end;

    Your Raise form_Trigger_failure Condition is not met hence not stopping the rest trigger body.

  • Check Conditions and exceptions

    I have the two tables:
    The first is a Chequing accounts table. This table has two
    number fields, the first is called balance, and the second is
    called Overdraft.
    The second is a Savings accounts table. This table has one
    field of importance, and that is the balance alone.
    I was thinking of making triggers that would fire after the
    transactions are executed. If the trigger finds that the either
    the Chequing balance is lesser than the negative overdraft, or
    if the Savings balance is lesser than 0, then the trigger would
    prevent the changes from being made.
    I was considering the use of a trigger that would spread an
    exception to the Transaction procedure. This would work as
    follows (Pardon me if the code has problems, I'm just writing
    from scratch):
    create trigger chq_acct after update on chq_acct for each row
    declare
    declare exception insufficient_funds here
    begin
    if balance < (- overdraft) then
    raise insuffiecient_funds
    end
    The things I need to know is how to pass this exception onto the
    procedure that is handling the update. However, I have not seen
    examples on how this is done yet.
    Another possibility is more modest and probably easier to
    handle: using a check constraint to verify data. In the savings
    acct, I can simply add a check constraint and if it is violated
    during update, catch the exception with the appropriate code
    (which I don't know what it is). However, I do not know about
    the check constraint for the Chequing accounts. It should be
    Balance > (- Overdraft), but this is a "variable" constraint as
    different customers have different levels of overdraft. Does
    Oracle handle this kind of constraint? If not, how should one
    handle it?
    Thanks

    hi....
    Trigger is a good idea.
    You can raise an application error in the trigger. Carefully
    define the two constants, ...msg and ...num - especially since
    the num should not be used for other exceptions. I usually put
    them in the specification of an PL/SQL block for global access.
    I added them to your code an example of what's needed.
    create trigger chq_acct after update on chq_acct for each row
    declare
    kc_sqlerrmsg_insufficientfunds CONSTANT VARCHAR2
    () := 'Insufficient funds.' ;
    kn_sqlerrmum_insufficientfunds CONSTANT NUMBER(5,0) := -
    20000 ; -- between -20000 and -20999
    begin
    if balance < (- overdraft) then
    --raise insuffiecient_funds
         RAISE_APPLICATION_ERROR ( kn_sqlerrmum_insufficientfunds
    kc_sqlerrmsg_insufficientfunds
    end Then when ever an update throws the exception, the query will be
    rolled back UNLESS you handle your new exception.
    Put your update query into an PL/SQL block with the exception.
    Be sure to tie the exception in the trigger using your defined
    exception and the all important ...num.
    The WHEN OTHERS THEN RAISE allows other exceptions to be handled
    by other, containg PL/SQL blocks.
    In the EXCEPTION part of the PL/SQL block, when the
    insuficient_funds exception is thrown, the variable SQLCODE will
    have your ...num in it and SQLERRM will have your ...msg in it.
    This is cool because when another UPDATE not in a block throws
    the exception in the trigger, whoever is trying to figure out
    what happened can use these!
    DECLARE
      kn_sqlerrmum_insufficientfunds CONSTANT NUMBER(5,0) := -
    20000 ; -- between -20000 and -20999
         insufficient_funds     EXCEPTION ;
         PRAGMA EXCEPTION_INIT ( insufficient_funds,
    kn_sqlerrmum_insufficientfunds )
    BEGIN
         UPDATE  .... SET balance .... ;
    EXCEPTION
         WHEN insufficient_funds THEN
          ...code to handle the potential overdraft...
         WHEN OTHERS THEN RAISE ;
    END ;
    Hope this helps. Good Luck.

  • PLL exception not propagating to form.

    Hello all,
    I am working in form 10g with database 9i. I am calling one Pll procedure from one trigger and handling the exception in the trigger outblock. But the trigger is not handling in the pll ORA-06502, Numeric or Value error. It is poping up the numeric or value error. In the exception part I have done code like below.
    exception when value_error then
    end;
    Can any body suggest, what is the error I am doing?
    Regards
    SUN

    It could be the exception itself is causing the ORA-06502.
    You need to find the source of the error and fix the problem rather than try to create an exception handler for it.
    Usually it is caused when a variable is defined that is too short to hold whatever data is being put into it.

  • Steps within Exception branch / Handler

    I'm trying to send an Alert message or close a S/A bridge within an Exception branch (handler) and none of these seems to works.
    Do any steps work at all in exception branch. Can we do some clean up work before the exception handler does its thing.
    A bit more on what I'm trying to do.
    1. Open S/A bridge via HTTP
    2. Transform source to target message of RFC type
    3. Make an RFC call in a branch with Exception handler and my exception is ABAP_Abend. We can call over 100 different ABAP programs via the RFC FM based on the data type and some programs Abend in SAP/target system
    4.If RFC is successful, map response to HTTP response and this works fine
    5. If RFC fails due to ABAP_Abend, I would like to close S/A bridge which waits indefinitely otherwise and send an alert message (via steps in exception handler). But this does not seem to work.
    Any ideas on how to take care of some clean up work before the system does the exception handling.
    Thank you for your responses.
    Parimala

    Hi,
    We use the S/A bridge and exception branch to trigger alerts upon error during synchronous calls. Our process gets terminated and the alerts are thrown upon exception. But we do not use RFC - we make http calls to third-party systems.
    I am not familiar with the ABAP-Abend. In case of Abend, do you still get a response back from the RFC? If you still get a response from RFC, you need to inspect the response (switch step is one way of doing it) and raise the exception to transfer the control to the exception branch.
    Regards,
    Suresh.

  • Trapping user defined exceptions in forms

    hi guys..
    i have a database trigger that goes like that:
    CREATE OR REPLACE TRIGGER loan_trig
    before insert on loans
    for each row
    declare
    lcount number(10);
    duplicate_loan exception;
    begin
    select count(1) into lcount from loans
    where branch_code=:new.branch_code
    and client_code=:new.client_code;
    if lcount>0 then
    raise duplicate_loan;
    end if;
    end;
    now how do i trap this user defined exception on the form ?
    declaring the exception in the form & handling it in the exeption handler section doesn't seem to work and i get the following error:
    "when button pressed trigger raised unhandled exception
    ora-06510"
    ora-06510:unhandled user-defined exception
    what am i doing wrong?
    if i have to handle the exception in the trigger body how do i do that?
    thankx in advance..

    Hello,
    raise duplicate_loan; won't avoid the trigger to insert and commit. If you really want to avoid the insertion, use the raise_applicatrion_error() instead.
    For instance : Raise_Application_Error( -20001, 'Insertion cannot be validated' );Then in your Forms module, you can catch it like this:
       Begin
       Insert Into ...
       when others then
          LC$Erreur := Substr( SQLERRM,1 , 500 );
          LN$err_code := ABS(SQLCODE);
          If LN$err_code = 20001 Then ...
       End;
    ...Francois

Maybe you are looking for