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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Try catch block sometimes crashes

Status
Not open for further replies.

TheVampire

Programmer
May 1, 2002
828
US
Why would a try / catch block work sometimes, and other times just stop the app ( in debug mode ) without generating an error?

In the code below, I try to find the highest number in a specific column. Sometimes the table is not ready to be opened, so I will get an error and have to wait and try again.

Sometimes, but not all of the time, when I get to the fill command, the execution will just halt, without throwing an error OR stepping into the catch code.

Any ideas? My debugger is set to break on all user unhandled exceptions.

Code:
Try
   Dim MyData As New DataTable
   Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT MAX (UniqueID) as [HighestID] from logbase", LDB)
   da.Fill(MyData)
   MyHighestID = MyData.Rows(0).Item(0).ToString
Catch
   'Code here to handle error

End Try
 
Followup: It also does this when compiled, and not just in the debugger. Application just stops and no exception message is displayed.
 
I'm not sure, but I will say that filling up a DataTable to get a single value is not best practice. Try this:

Code:
Try
  Dim CMD As New OledbCommand
  CMD.CommandText = "SELECT MAX (UniqueID) as [HighestID] from logbase"
  CMD.Connection = SomeConnection
  MyHighestID = CMD.ExecuteScalar()
Catch
   'Code here to handle error

End Try
 
Thanks for pointing that out about using the ExecuteScalar command.

I tried it with this and still the app halted at the same spot without an exception error.

If anyone else has ideas please let me know. This is quite strange.
 
Have you made any changes in the Exceptions Dialog box (Ctrl+Alt+E)?
 
I guess that's what you meant by "My debugger is set to break on all user unhandled exceptions".
 
If the table is not ready to be opened, what error are you seeing?

When you say the execution will just halt, how do you know the program just hasn't ended normally?

Have you tried placing a Finally section in your Try statement with a message box to indicate you've gotten there successfully?
 
Just a thought...

If your app appears to freeze, without it actually "crashing", it could be that the select statement is getting blocked by other database processes.

For example, you are trying to get the max(UniqueId). If another process is inserting or deleting rows in this table, it's possible that the table is locked by that other process, and will not return until the other process is done (and it releases the lock).

If your database is SQL Server, you could try running the sp_who2 command in a query window to see what the other process is, and more importantly, what the command the other process is running.

If your database is NOT SQL Server, this could still be your problem, but there are likely to be other commands that you can run to see if my assumption/theory is correct.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks for the tips and I'm just reporting that I found where the problem was. I had a DLL that I had made a method call on earlier before I tried to open the DataAdapter. The DLL was actually causing the crash without an exception message (sometimes) because it was still in the process of reindexing the same database that I was trying to open with the DataAdapter. I'm working with paradox ver 7 database files and sometimes I have to make calls to the Borland Database Engine to access features that I can't reach directly under .NET.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top