Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scope and exception

Status
Not open for further replies.

michaelkrauklis

Programmer
Dec 5, 2001
226
US
Two quick(I hope) questions.
Exception: I'm using ODBC and the CDatabase object to query a SQL Database. My problem is that when I pass SQL an invalid date my program crashes. I catch the exception but it's not any type of exception I would be expecting such as a CDBException. I catch using catch(...). And even if I don't exit in my exception handling my program dies, and dies hard. How can I tell what kind of exception is being thrown? It only happens when there is an invalid date in my dynamicly created sql insert statement. And I'd like to still know when this happens so I can log the error and have someone go in and fix the invalid dates...

Scope: I'm a bit rusty on the scope of variables when you use pointers. Here's an example:
Code:
int *i;
if(true){
  int z=52;
  i=&z;
}
Now has the memory for z been freed, thus making i an invalid pointer? Or, since there is something pointing to it does the memory stay in scope and I would eventually have to delete it?

This comes into play in my error handling. I want to print out the dynamicly created SQL statement in my error message but I create it inside my try, so subsequently it goes out of scope when there is an error and I break to the catch case. So I made a pointer outside the try block, then assign it to the address of the SQL statement inside the try block. In my mind the variable should be out of scope so it is just by chance that this method has worked so far because the SQL statement hasn't been written in memory. Am I right here? And if so is there a way to keep my variable in scope other than creating it outside the try block which is a bit costly in my case or copying it to another variable that is declared out of the try block?

Any help will be greatly appreciated. Sorry for the simple questions, but it's been a while since I've done C++ and I'm a bit rusty. Thanks! MY[red]enigma[/red]SELF:-9
myenigmaself@yahoo.com
 
With the exception I would first catch a CDBException* then a CException. Look at the error message and see what it says. If you dont catch it then windows will catch it and handle it with the catch all.

The scope of z is gone and though the data may still exist in that current location, it can not be assumed to ALWAYS be correct.


Matt
 
Yeah, that's what I thought about the scope. I just wanted to make sure. As for the exception, I already catch a CDBException, then a CException, than an exception, than (...) It finally catches on the (...) but my program still fails. Debugger prints out this:
Code:
//*******************************************
The conversion of a char data type to a datetime data type resulted in an out-of
-range datetime value.
State:22008,Native:242,Origin:[Microsoft][ODBC SQL Server Driver][SQL Server]

The statement has been terminated.
State:01000,Native:3621,Origin:[Microsoft][ODBC SQL Server Driver][SQL Server]

First-chance exception in ParadoxToSQL.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in ParadoxToSQL.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in ParadoxToSQL.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in ParadoxToSQL.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in ParadoxToSQL.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in ParadoxToSQL.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
//*******************************************
And then my program crashes. Am I supposed to be catching a CDBException* rather than a CDBException? MY[red]enigma[/red]SELF:-9
myenigmaself@yahoo.com
 
Ok, using CDBException *e worked, I caught the error. Why do you have to use a pointer? It doesn't say anything about that in the documentation... do you always have to use pointers when catching errors? And my program still crashes after I've printed out the appropriate error message. Any ideas? MY[red]enigma[/red]SELF:-9
myenigmaself@yahoo.com
 
All CException (and derived exceptions) are thrown as pointers (at least from what I have seen). Everything seems to have been thrown as a "new CException" etc... Also, because the "new" was used and "delete" will not work on it, you need to call ex->Delete() to clean up.

Matt
 
Thanks for the great info. Still doesn't work though. I catch the exception fine but I get an assertion error after that. If I choose to ignore the error I can do it a few times, but after too many it just won't break out of the assertion error dialog box. This doesn't make sense to me... MY[red]enigma[/red]SELF:-9
myenigmaself@yahoo.com
 
Where does it take you when you "RETRY" the assertion? and what is causing it? is it the call to DELETE? See if you can trace back the "throw" on the call stack and see where it is coming from. In the worst case you could write a Validate function to return true or false if the date is valid.

Matt
 
Got it! I traced it back to an assertion in dbcore.cpp and the assertion was if there was already a transaction waiting. I just call Rollback on the CDatabase object when there is a CDBException now, which clears the pending transaction(that actually failed) and it works now. I also found another error where my input buffer was too small which was screwing up my parsing, but that wasn't directly related to this. Thanks for all your help:) MY[red]enigma[/red]SELF:-9
myenigmaself@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top