Solved

how to throw a sqlexception in c++

Posted on 2003-12-02
10
1,782 Views
Last Modified: 2012-06-27
I am using c++ class to access oracle and wants to throw an error when no rows are updated. here is my code

      try {
            env = Environment::createEnvironment(Environment::DEFAULT);
            conn = env->createConnection("shiran","dikky","SPECTRUM");
      
            stmt = conn->createStatement("update EMP2 set VACATION='Hilton' where SAL=1000");
            stmt->setAutoCommit(FALSE);

            rs = stmt->executeQuery();
      
            int iNum = stmt->getUpdateCount();
            cout<< iNum << " rows updated" << endl;

            SQLException error;
            throw(error);

            conn->terminateStatement(stmt);
            env->terminateConnection(conn);
            Environment::terminateEnvironment(env);

      } catch (SQLException ex)
      {
            cout <<  ex.getMessage() << "\n" ;
      }

This gives a compilation error I want to know how to do this. Can some one please help
Thanks
afernando
0
Comment
Question by:afernando
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:DrJekyll
ID: 9857929
What is your compile error?  Do you have all the necessary header included. I am speaking from a C environment not a C++

DJ
0
 
LVL 23

Expert Comment

by:seazodiac
ID: 9857986
I am not sure what exactly you are looking for here, a  working, sample code as to how to throw an exception in c++ or the LOGIC to do that.

although I am NOT good at C++, I can only tell you the logic and where went wrong in your code by way of my JAVA and PERL knowledge.

first off, let me point out where your compilation error arise from,

comment the following two lines:

/**two mistakes:
 *1. the explicitly declared SQLException "error" is NULL at this point since you did not initilize it. and compiler will compile since you throw an NULL exception.
 *2. this probably is NOT your intention after all. because there is no conditional logic here. ie., the runtime will throw this error exception regardless of the results of
 * update.
       SQLException error;
        throw(error);
*/

You should change the above two lines to something like this:

IF (iNum =0){
SQLException zero_update_error = new SQLException("Zero row has been updated!");
throw(zero_update_error);
}


So the final code piece should look like this (you need to catch the thrown exception explicitly too in catch clause)


    try {
         env = Environment::createEnvironment(Environment::DEFAULT);
         conn = env->createConnection("shiran","dikky","SPECTRUM");
   
          stmt = conn->createStatement("update EMP2 set VACATION='Hilton' where SAL=1000");
         stmt->setAutoCommit(FALSE);

         rs = stmt->executeQuery();
   
          int iNum = stmt->getUpdateCount();
         cout<< iNum << " rows updated" << endl;

         IF (iNum =0){
         SQLException zero_update_error = new SQLException("Zero row has been updated!");
         throw(zero_update_error);
         }
         conn->terminateStatement(stmt);
         env->terminateConnection(conn);
         Environment::terminateEnvironment(env);

    } catch (SQLException zero_update_error)
    {
         cout <<  ex.getMessage() << "\n" ;
    }

     catch (SQLException ex)
    {
         cout <<  ex.getMessage() << "\n" ;
    }


Once again, I am not sure of my C++ syntax, but to this point, you should be able to understand what I am pointing you to....

if you are well versed in PL/SQL, you can do this Exception raising and handling in PL/SQL too. but in your case, doing it in application seems easier.

hope this helps

0
 
LVL 48

Expert Comment

by:schwertner
ID: 9858210
try {
         env = Environment::createEnvironment(Environment::DEFAULT);
         conn = env->createConnection("shiran","dikky","SPECTRUM");
   
          stmt = conn->createStatement("update EMP2 set VACATION='Hilton' where SAL=1000");
         stmt->setAutoCommit(FALSE);

         rs = stmt->executeQuery();
   
          int iNum = stmt->getUpdateCount();
         cout<< iNum << " rows updated" << endl;

         SQLException error;
         throw(error);

         conn->terminateStatement(stmt);
         env->terminateConnection(conn);
         Environment::terminateEnvironment(env);

    } catch (SQLException ex)
    {
         cout <<  ex.getMessage() <<  endl;
    }

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:afernando
ID: 9863562
In my earlier code I did not get any compile errors only run time errors . How ever with the code from  seazodiac I changed the code as

if(iNum == 0)
{
       SQLException *zero_update_error = new SQLException();
       zero_update_error->setErrorCtx("Zero row has been updated!");
       throw(zero_update_error);
}

But it still gives run time error on the setErrorCtx line
Any idea ???
0
 
LVL 23

Expert Comment

by:seazodiac
ID: 9863765
did you put the CATCH block on the zero_update_error exception?
In java, when you explicitly throw an exception, you have to catch and handle it in the catch block.

  //put the zero_update_error exception declaration up there
  SQLException *zero_update_error = new SQLException();
  zero_update_error->setErrorCtx("Zero row has been updated!");
   try {
        env = Environment::createEnvironment(Environment::DEFAULT);
        conn = env->createConnection("shiran","dikky","SPECTRUM");
   
         stmt = conn->createStatement("update EMP2 set VACATION='Hilton' where SAL=1000");
        stmt->setAutoCommit(FALSE);

        rs = stmt->executeQuery();
   
         int iNum = stmt->getUpdateCount();
        cout<< iNum << " rows updated" << endl;

        IF (iNum ==0){
                throw(zero_update_error);
        }
        conn->terminateStatement(stmt);
        env->terminateConnection(conn);
        Environment::terminateEnvironment(env);

   } catch (SQLException zero_update_error)
   {
        cout <<  ex.getMessage() << "\n" ;
   }

    catch (SQLException ex)
   {
        cout <<  ex.getMessage() << "\n" ;
   }


I believe you can copy and paste to use my code....
Hope this helps
0
 

Author Comment

by:afernando
ID: 9864658
I tried the last way still it gives a run time error at

zero_update_error->setErrorCtx("Zero row has been updated!");

There fore program terminates with out going into try catch block

Any idea ???
0
 
LVL 23

Accepted Solution

by:
seazodiac earned 500 total points
ID: 9866609
ok afernando:

I am certain this is a C++ programming syntax error.
Is this the correct way of declaring and initializing a new exception?
       SQLException *zero_update_error = new SQLException();
       zero_update_error->setErrorCtx("Zero row has been updated!");

why don't you make an EXCEPTION class that construct the zero update exception?

This runtime error must be because you did not catch it when you explicitly throw it.

how about adding a general exception handler in the end of the code block like below?

catch(Exception e){}
0
 
LVL 5

Expert Comment

by:DrJekyll
ID: 9867192
Can you try using something like the following. It obviously has to come before
your sql

 EXEC SQL WHENEVER NOT FOUND GOTO err_exit;

or

 EXEC SQL WHENEVER NOT FOUND DO process_error;

I know GOTO is verboten but in this case I prefer it.

At the end of main or other function I usually put

/* Failed exit*/
err_exit:

   EXEC SQL WHENEVER SQLERROR CONTINUE;
   EXEC SQL WHENEVER NOT FOUND CONTINUE;
/* my own error reporting routine */
   LogError(txt);  
   EXEC SQL ROLLBACK WORK RELEASE;
   return(1);

I think C++ support this.

DJ
0
 

Author Comment

by:afernando
ID: 9879453
Hey seazodiac

I think your final suggestion is the best. Thanks
0
 
LVL 23

Expert Comment

by:seazodiac
ID: 9879506
you are very welcome. Remember to clean up this post and accept the answer.

Happy coding...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question