Dovberman
asked on
Try Catch Timeout
I am getting a timeout error somewhere in a code block. The block has about 40 lines of code.
What is the proper way to catch the error?
try
{
//Code Block
}
catch (Exception err)
{
Session["StackTrace"] = err.StackTrace;
Session["Message"] = err.Message;
Session["Data"] = err.Data.ToString();
Session["Source"] = err.Source;
Session["ErrProcName"] = "Default_Page_Load";
Response.Redirect("~/Error Handling/S howError.a spx");
//return "Process OK";
}
What is the proper way to catch the error?
try
{
//Code Block
}
catch (Exception err)
{
Session["StackTrace"] = err.StackTrace;
Session["Message"] = err.Message;
Session["Data"] = err.Data.ToString();
Session["Source"] = err.Source;
Session["ErrProcName"] = "Default_Page_Load";
Response.Redirect("~/Error
//return "Process OK";
}
Do you get the timeout within Visual Studio, or only once deployed? Are you running any long-running operations anywhere?
ASKER
The timeout occurs when deployed. I am not sure if it occurs within Visual Studio.
Yes there are 3 long running operations within the code block. I need to find out which one is timing out. The catch mode is not getting triggered.
I usually do not ask for my code to be reviewed. However, I am stuck here. This is one of the 3 long operations. Should I wrap the other 2 in a try catch block?
-------------
try
{
SqlCommand cmdapdExceptions = new SqlCommand("usp_apdExcepti ons", conStockSelect);
cmdapdExceptions.CommandTy pe = System.Data.CommandType.St oredProced ure;
cmdapdExceptions.CommandTi meout = 2400;
// Append the Exceptions table
for (int i = 0; i < intRow; i++)
{
cmdapdExceptions.Parameter s.Clear();
cmdapdExceptions.Parameter s.Add("@Sy mbolID", SqlDbType.Int).Value = dblArrException[i, 0];
cmdapdExceptions.Parameter s.Add("@Cu rr5DayAvg" , SqlDbType.Float).Value = dblArrException[i, 1];
cmdapdExceptions.Parameter s.Add("@Pr ev8DayAvg" , SqlDbType.Float).Value = dblArrException[i, 2];
RunProcedure(cmdapdExcepti ons, "usp_apdExceptions");
} // End for (int i = 0; i < intRow; i++)
cmdapdExceptions.Dispose() ;
// Delete from Exceptions WHERE (Curr5DayAvg - Prev8DayAvg) =0
// OR PctChg >= 100
strDelExceptionsSQL = "DELETE FROM Exceptions ";
strDelExceptionsSQL += "WHERE (Curr5DayAvg - Prev8DayAvg) =0 OR (Curr5DayAvg - Prev8DayAvg)/(Prev8DayAvg) >= 1";
SqlCommand cmdDelExceptions1 = new SqlCommand("DelExceptions" , conStockSelect);
cmdDelExceptions.CommandTy pe = System.Data.CommandType.Te xt;
cmdDelExceptions.CommandTe xt = strDelExceptionsSQL;
cmdDelExceptions.CommandTi meout = 2400;
RunProcedure(cmdDelExcepti ons, "DelExceptions1");
cmdDelExceptions.Dispose() ;
// Execute query to update the PctChg
string strUpdPctChgSQL = "UPDATE Exceptions ";
strUpdPctChgSQL += "SET PctChg = 100 * (Curr5DayAvg - Prev8DayAvg)/(Prev8DayAvg) ";
SqlCommand cmdUpdPctChg = new SqlCommand("UpdPctChg", conStockSelect);
cmdUpdPctChg.CommandType = System.Data.CommandType.Te xt;
cmdUpdPctChg.CommandText = strUpdPctChgSQL;
cmdUpdPctChg.CommandTimeou t = 2400;
RunProcedure(cmdUpdPctChg, "UpdPctChg");
cmdUpdPctChg.Dispose();
// Get the MAX(PctChg)
string strMaxPctChgSQL = "SELECT MAX(PctChg) As MaxPctChg FROM Exceptions";
string strMaxPctChg = GetColValue(conStockSelect , strMaxPctChgSQL, "MaxPctChg", "String");
double dblMaxPctChg = double.Parse(strMaxPctChg) ;
// Get the ExceptionCount
string strExceptionCountSQL = "SELECT Count(*) As ExceptionCount FROM Exceptions";
string strExceptionCount = GetColValue(conStockSelect , strExceptionCountSQL, "ExceptionCount", "String");
int intExceptionCount = int.Parse(strExceptionCoun t);
if (conStockSelect.State == ConnectionState.Open)
{
conStockSelect.Close();
}
// Update SiteURL and refDate
//Fix
string strUpdExceptionsSQL = "UPDATE Exceptions SET SiteURL = '" + ResearchSiteURL + "', refDate = '" + dteSampleABStart.ToShortDa teString() + "' ";
SqlCommand cmdUpdExceptions = new SqlCommand("UpdExceptions" , conStockSelect);
cmdUpdExceptions.CommandTy pe = System.Data.CommandType.Te xt;
cmdUpdExceptions.CommandTe xt = strUpdExceptionsSQL;
cmdUpdExceptions.CommandTi meout = 2400;
cmdUpdExceptions.Connectio n = conStockReduce;
if (conStockReduce.State == ConnectionState.Closed)
{
conStockReduce.Open();
}
RunProcedure(cmdUpdExcepti ons, "UpdExceptions");
cmdUpdExceptions.Dispose() ;
// Stop the clock
DateTime endDT = DateTime.Now;
TimeSpan ts = endDT.Subtract(startDT);
lblExceptionStatus.Text = ts.Minutes.ToString() + " : " + ts.Seconds.ToString() + ": " + intExceptionCount.ToString () + " Exceptions Built";
if (conStockSelect.State == ConnectionState.Open)
{
conStockSelect.Close();
}
}
catch (Exception err)
{
Session["StackTrace"] = err.StackTrace;
Session["Message"] = err.Message;
Session["Data"] = err.Data.ToString();
Session["Source"] = err.Source;
Session["ErrProcName"] = "Default_Page_Load";
Response.Redirect("~/Error Handling/S howError.a spx");
//return "Process OK";
}
}
Yes there are 3 long running operations within the code block. I need to find out which one is timing out. The catch mode is not getting triggered.
I usually do not ask for my code to be reviewed. However, I am stuck here. This is one of the 3 long operations. Should I wrap the other 2 in a try catch block?
-------------
try
{
SqlCommand cmdapdExceptions = new SqlCommand("usp_apdExcepti
cmdapdExceptions.CommandTy
cmdapdExceptions.CommandTi
// Append the Exceptions table
for (int i = 0; i < intRow; i++)
{
cmdapdExceptions.Parameter
cmdapdExceptions.Parameter
cmdapdExceptions.Parameter
cmdapdExceptions.Parameter
RunProcedure(cmdapdExcepti
} // End for (int i = 0; i < intRow; i++)
cmdapdExceptions.Dispose()
// Delete from Exceptions WHERE (Curr5DayAvg - Prev8DayAvg) =0
// OR PctChg >= 100
strDelExceptionsSQL = "DELETE FROM Exceptions ";
strDelExceptionsSQL += "WHERE (Curr5DayAvg - Prev8DayAvg) =0 OR (Curr5DayAvg - Prev8DayAvg)/(Prev8DayAvg)
SqlCommand cmdDelExceptions1 = new SqlCommand("DelExceptions"
cmdDelExceptions.CommandTy
cmdDelExceptions.CommandTe
cmdDelExceptions.CommandTi
RunProcedure(cmdDelExcepti
cmdDelExceptions.Dispose()
// Execute query to update the PctChg
string strUpdPctChgSQL = "UPDATE Exceptions ";
strUpdPctChgSQL += "SET PctChg = 100 * (Curr5DayAvg - Prev8DayAvg)/(Prev8DayAvg)
SqlCommand cmdUpdPctChg = new SqlCommand("UpdPctChg", conStockSelect);
cmdUpdPctChg.CommandType = System.Data.CommandType.Te
cmdUpdPctChg.CommandText = strUpdPctChgSQL;
cmdUpdPctChg.CommandTimeou
RunProcedure(cmdUpdPctChg,
cmdUpdPctChg.Dispose();
// Get the MAX(PctChg)
string strMaxPctChgSQL = "SELECT MAX(PctChg) As MaxPctChg FROM Exceptions";
string strMaxPctChg = GetColValue(conStockSelect
double dblMaxPctChg = double.Parse(strMaxPctChg)
// Get the ExceptionCount
string strExceptionCountSQL = "SELECT Count(*) As ExceptionCount FROM Exceptions";
string strExceptionCount = GetColValue(conStockSelect
int intExceptionCount = int.Parse(strExceptionCoun
if (conStockSelect.State == ConnectionState.Open)
{
conStockSelect.Close();
}
// Update SiteURL and refDate
//Fix
string strUpdExceptionsSQL = "UPDATE Exceptions SET SiteURL = '" + ResearchSiteURL + "', refDate = '" + dteSampleABStart.ToShortDa
SqlCommand cmdUpdExceptions = new SqlCommand("UpdExceptions"
cmdUpdExceptions.CommandTy
cmdUpdExceptions.CommandTe
cmdUpdExceptions.CommandTi
cmdUpdExceptions.Connectio
if (conStockReduce.State == ConnectionState.Closed)
{
conStockReduce.Open();
}
RunProcedure(cmdUpdExcepti
cmdUpdExceptions.Dispose()
// Stop the clock
DateTime endDT = DateTime.Now;
TimeSpan ts = endDT.Subtract(startDT);
lblExceptionStatus.Text = ts.Minutes.ToString() + " : " + ts.Seconds.ToString() + ": " + intExceptionCount.ToString
if (conStockSelect.State == ConnectionState.Open)
{
conStockSelect.Close();
}
}
catch (Exception err)
{
Session["StackTrace"] = err.StackTrace;
Session["Message"] = err.Message;
Session["Data"] = err.Data.ToString();
Session["Source"] = err.Source;
Session["ErrProcName"] = "Default_Page_Load";
Response.Redirect("~/Error
//return "Process OK";
}
}
Does the RunProcedure method have a try/catch?
ASKER
This sounds like I need to wrap each RunProcedure in a try/catch.
Is that correct?
Is that correct?
You said, "The catch mode is not getting triggered." I was wondering if RunProcedure had a try/catch that was handling the exception, hence you not seeing it within the method shown above.
ASKER
I wrapped each RunProcedure.
The custom error page is not displayed.
Just this:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Debugging is on in web.config.
The custom error page is not displayed.
Just this:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Debugging is on in web.config.
ASKER
You are right. RunProcedure has a TryCatch.
I am wondering why the custom error page does not display.
private string RunProcedure(SqlCommand cmd, string strProcName)
{
try
{
cmd.ExecuteNonQuery();
return "Process OK";
}
catch (Exception err)
{
Session["StackTrace"] = err.StackTrace;
Session["Message"] = err.Message;
Session["Data"] = err.Data.ToString();
Session["Source"] = err.Source;
Session["ErrProcName"] = strProcName;
Response.Redirect("~/Error Handling/S howError.a spx");
return "Process OK";
}
}
I am wondering why the custom error page does not display.
private string RunProcedure(SqlCommand cmd, string strProcName)
{
try
{
cmd.ExecuteNonQuery();
return "Process OK";
}
catch (Exception err)
{
Session["StackTrace"] = err.StackTrace;
Session["Message"] = err.Message;
Session["Data"] = err.Data.ToString();
Session["Source"] = err.Source;
Session["ErrProcName"] = strProcName;
Response.Redirect("~/Error
return "Process OK";
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This was already in web.config
<customErrors
mode="Off" defaultRedirect="~/ErrorHa ndling/Sho wError.asp x">
</customErrors>
I may need to debug the old fashioned way by commenting out blocks of code.
<customErrors
mode="Off" defaultRedirect="~/ErrorHa
</customErrors>
I may need to debug the old fashioned way by commenting out blocks of code.
ASKER
The issue is a stored procedure that returns no rows.
ASKER
The issue is a stored procedure that returns no rows.
Thanks for your help.
Thanks for your help.