afernando
asked on
how to throw a sqlexception in c++
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::createEnviron ment(Envir onment::DE FAULT);
conn = env->createConnection("shi ran","dikk y","SPECTR UM");
stmt = conn->createStatement("upd ate 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(s tmt);
env->terminateConnection(c onn);
Environment::terminateEnvi ronment(en v);
} 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
try {
env = Environment::createEnviron
conn = env->createConnection("shi
stmt = conn->createStatement("upd
stmt->setAutoCommit(FALSE)
rs = stmt->executeQuery();
int iNum = stmt->getUpdateCount();
cout<< iNum << " rows updated" << endl;
SQLException error;
throw(error);
conn->terminateStatement(s
env->terminateConnection(c
Environment::terminateEnvi
} 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
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::createEnviron ment(Envir onment::DE FAULT);
conn = env->createConnection("shi ran","dikk y","SPECTR UM");
stmt = conn->createStatement("upd ate 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(s tmt);
env->terminateConnection(c onn);
Environment::terminateEnvi ronment(en v);
} 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
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::createEnviron
conn = env->createConnection("shi
stmt = conn->createStatement("upd
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(s
env->terminateConnection(c
Environment::terminateEnvi
} 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
try {
env = Environment::createEnviron ment(Envir onment::DE FAULT);
conn = env->createConnection("shi ran","dikk y","SPECTR UM");
stmt = conn->createStatement("upd ate 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(s tmt);
env->terminateConnection(c onn);
Environment::terminateEnvi ronment(en v);
} catch (SQLException ex)
{
cout << ex.getMessage() << endl;
}
env = Environment::createEnviron
conn = env->createConnection("shi
stmt = conn->createStatement("upd
stmt->setAutoCommit(FALSE)
rs = stmt->executeQuery();
int iNum = stmt->getUpdateCount();
cout<< iNum << " rows updated" << endl;
SQLException error;
throw(error);
conn->terminateStatement(s
env->terminateConnection(c
Environment::terminateEnvi
} catch (SQLException ex)
{
cout << ex.getMessage() << endl;
}
ASKER
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->setErro rCtx("Zero row has been updated!");
throw(zero_update_error);
}
But it still gives run time error on the setErrorCtx line
Any idea ???
if(iNum == 0)
{
SQLException *zero_update_error = new SQLException();
zero_update_error->setErro
throw(zero_update_error);
}
But it still gives run time error on the setErrorCtx line
Any idea ???
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->setErro rCtx("Zero row has been updated!");
try {
env = Environment::createEnviron ment(Envir onment::DE FAULT);
conn = env->createConnection("shi ran","dikk y","SPECTR UM");
stmt = conn->createStatement("upd ate 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(s tmt);
env->terminateConnection(c onn);
Environment::terminateEnvi ronment(en v);
} 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
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->setErro
try {
env = Environment::createEnviron
conn = env->createConnection("shi
stmt = conn->createStatement("upd
stmt->setAutoCommit(FALSE)
rs = stmt->executeQuery();
int iNum = stmt->getUpdateCount();
cout<< iNum << " rows updated" << endl;
IF (iNum ==0){
throw(zero_update_error);
}
conn->terminateStatement(s
env->terminateConnection(c
Environment::terminateEnvi
} 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
ASKER
I tried the last way still it gives a run time error at
zero_update_error->setErro rCtx("Zero row has been updated!");
There fore program terminates with out going into try catch block
Any idea ???
zero_update_error->setErro
There fore program terminates with out going into try catch block
Any idea ???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Hey seazodiac
I think your final suggestion is the best. Thanks
I think your final suggestion is the best. Thanks
you are very welcome. Remember to clean up this post and accept the answer.
Happy coding...
Happy coding...
DJ