Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi All,

I have searched a lot on this topic. But all in vain.

Kindly advice me, how to catch exceptions which are related to connection lost(both SQL and Oracle) problem.

Thanks in advance.
Posted
Updated 15-Oct-14 23:07pm
v2
Comments
ZurdoDev 15-Oct-14 11:15am    
Where are you stuck? You can catch Exception or you can catch SqlException, etc.
PIEBALDconsult 15-Oct-14 11:38am    
If you mean, only when trying to create/open the connection then, put only those statements (and nothing else) in a try/catch
Maciej Los 15-Oct-14 15:47pm    
What you mean? Is there any reason to catch only exceptions related to the "connection"?

You can handle specific exceptions like this.
C#
Try {
   // code here
}
catch (SqlException odbcEx) {
   // Handle SQL related excetion here, you can catch more specific exception inside this.
}

 catch (OracleException e)
    // Handle Oracle related exceptions
   {

catch (Exception ex) {
   // Handle generic exceptions here
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Oct-14 12:18pm    
It would be good, but bad part is catching System.Exception. You create a false impression that all exceptions should be caught. Wrong. Exceptions are structured. Just remove this part; and other exceptions will be propagated up the stack, the be handled somewhere on top (eventually, all of them should be caught). Catching too locally is a big fallacy.
—SA
furynight8 16-Oct-14 3:53am    
Hi All,

First of all, thanks a lot for all your replies.

I am having the below code in my application, to catch row level exceptions.

OracleCommand actionRtfCommand = new OracleCommand("select * from ACTION_RTF", conn);
actionRtfCommand.InitialLONGFetchSize = -1;
OracleDataReader actionRtfReader = actionRtfCommand.ExecuteReader();

// read only one record each roundtrip to avoid that exception has consequences for subsequent read statements
// (when the exception occurs the buffer becomes corrupted giving problems for subsequent read operations to the same buffer)
// Since this reduction of fetchsize (increasing number of roundtrips) has a significant performance penalty (typical fetchsize is 131072)
// it should only be applied when command involves LONG or LONG RAW columns
actionRtfReader.FetchSize = actionRtfReader.RowSize;

bool endOfFile = !actionRtfReader.HasRows;
while (!endOfFile)
{
try
{
bool readResult = actionRtfReader.Read();
endOfFile = !readResult;
}
catch (Exception rowLevelException)
{
// handle exception
MessageBox.Show(rowLevelException.GetType().ToString() + " : " + rowLevelException.Message);
}
}

DataReader will fetch only one row at a time and if any exceptions occurs while reading it will be caught in the exception.

Sometimes while executing the loop, the connection to the database is lost due to some reason. So only in condition when the connection to the database is lost, i have to terminate the program. In other conditions, it should work as it is working now.
NaibedyaKar 16-Oct-14 3:58am    
Why don't you add one condition there to check the database connection before doing any task.
furynight8 16-Oct-14 4:11am    
Hi,

I am checking the connection while saving the details in DataReader. I have millions of records to be fetched from database. Since DataReader is a connected object, I am getting exception in each objDataReader.Read() statement. For example, say i have fetched around 5000 rows from database using the Read() statement and the connection to database is lost while fetching 5001 row.

There may be many exceptions while objDatareader.Read(), but only when the exception occurs due to connection lost, i have to terminate my program.

Kindly advice me how to proceed in this.
furynight8 16-Oct-14 5:13am    
I have tried to solve my problem by below approach.

bool endOfFile = !actionRtfReader.HasRows;
while (!endOfFile)
{
try
{
bool readResult = actionRtfReader.Read();
endOfFile = !readResult;
}
catch (Exception rowLevelException)
{
if (ex.Message.ToString().ToLower().Contains("connect"))
{
Environment.Exit(0);
}
else
{
MessageBox.Show(rowLevelException.GetType().ToString() + " : " + rowLevelException.Message);
}
}
}

I am checking whether the exception message contains "connect". If it contains "connect" , i am terminating my program.

Kindly advise me whether my approach is correct.
Hi All,

I have tried to solve my problem by below approach.

bool endOfFile = !actionRtfReader.HasRows;
while (!endOfFile)
{
try
{
bool readResult = actionRtfReader.Read();
endOfFile = !readResult;
}
catch (Exception rowLevelException)
{
if (ex.Message.ToString().ToLower().Contains("connect"))
{
Environment.Exit(0);
}
else
{
MessageBox.Show(rowLevelException.GetType().ToString() + " : " + rowLevelException.Message);
}
}
}

I am checking whether the exception message contains "connect". If it contains "connect" , i am terminating my program.

Kindly advise me whether my approach is correct.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900