There are quite a few things you can do.
1> In your code use try..catch..finally block.
pulic void someFunction()
{
try
{
}
catch(Exception ex)
{
throw ex;
}
finally
{
}
}
2> In your Global.asax file you can log unhandled exception. I have used Log4Net Logging library.
protected void Application_Error(object sender, EventArgs e)
{
System.Web.HttpContext context = HttpContext.Current;
Exception ex = context.Server.GetLastError().GetBaseException();
if (!ex.Message.Contains("Invalid length for a Base-64 char array"))
{
if (!(context.Request.FilePath.ToString().ToUpper().Contains(("ScriptResource.axd").ToUpper()) || context.Request.FilePath.ToString().ToUpper().Contains(("WebResource.axd").ToUpper())))
{
CLogger.WriteLog(ELogLevel.ERROR, "URL : " + context.Request.Url.ToString() + "\n");
CLogger.WriteLog(ELogLevel.ERROR, "FILE : " + context.Request.FilePath.ToString() + "\n");
CLogger.WriteLog(ELogLevel.ERROR, "USER : " + context.User.Identity.Name + "\n");
CLogger.WriteLog(ELogLevel.ERROR, "MESSAGE : " + ex.Message + "\n");
CLogger.WriteLog(ELogLevel.ERROR, "SOURCE : " + ex.Source + "\n");
CLogger.WriteLog(ELogLevel.ERROR, "STACK TRACE : " + ex.StackTrace + "\n");
CLogger.WriteLog(ELogLevel.ERROR, "INNER EXCEPTION : " + ex.InnerException + "\n");
CLogger.WriteLog(ELogLevel.ERROR, "---------------------------------------------------------------------------------------------\n");
}
}
}
for details check
Clickty [
^]
3> For database specific connection handling use "using statement".
4>
Two things : in your web.config add
<customerrors mode="On" defaultredirect="~/Error.aspx" redirectmode="ResponseRedirect"></customerrors>
and set
<compilation debug="false">
in your production version to redirect users to error page.</compilation>