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

How to handle Connection Timeout exception

Status
Not open for further replies.

PGoRule

Programmer
Jul 26, 2006
438
0
0
IN
Hi,

In one of the module, we need to handle Connection Timeout exception. But there are no such exceptions available, so we are using GetType() of the exception to display appropriate message that Connection Timeout has occurred.
Code:
catch (Exception e)
{
if (e.GetType() == typeof(System.Data.Odbc.OdbcException))
{
ErrMsg = "Timeout Exception";
}
}
We tested it on Oracle, the e.InnerException.GetType() gives typeof(System.Data.Odbc.OdbcException). But when we use the same code on SQL server, the innerexception returns null and e.GetType() returns typeof(System.Exception). So how to handle this timeout exception for different databases?

Thanx,

Prasahnt
 
I'm pretty sure that this

Code:
if (e.GetType() == typeof(System.Data.Odbc.OdbcException))
{
ErrMsg = "Timeout Exception";
}

is not true, it's not because it return an odbcexception that it will be due to a timeout. It could be many other things.

but you could and better do this

Code:
catch (System.Data.Odbc.OdbcException e)
{
ErrMsg = "Timeout Exception : " & e.Message();
}
catch (Exception e)
{
ErrMsg = "Other Exception : " & e.Message();
}

Christiaan Baes
Belgium

"My old site" - Me
 
Hi,

Thanx Chrissie for your help.

As the requirement was to append a error message of "Timeout Exception" to the earlier error message, I handled that inside one catch block only, so as to minimise string operations.
Here is my solution. This solves my purpose on Oracle and SQL DB...Plz. take a look at it and let me know...
Code:
catch (Exception e)
{
  LogError("Exception: " + e.ToString());
  if (e.InnerException.GetType() == typeof(System.Data.Odbc.OdbcException))
  {
    LogError("Timeout Exception: The exception may have occured due to connection Timeout. Please check value of TIMEOUT parameter.");
  }
...
...
}

Sharing the best from my side...

--Prashant--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top