如何实现多个try块 [英] How can I implement multiple try blocks

查看:83
本文介绍了如何实现多个try块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于大家来说,如果一个方法抛出异常,其他方法将无法运行。这是我使用单独的try..catch块的主要原因。所以,即使一个函数给出了异常,下一个函数也可以执行。



你能不能给我一个很好的方法,而不是我在这里用来执行下一个函数即使在第一个功能期间发生异常。



我的代码



For you all, "if one method throws an exception, the other methods will not run." this is the main reason I am using separate try..catch blocks. So that, even if a function gives an exception, the next can execute.

Can you suggest me a nice approach other than I am using here to execute the next function even if an exception occurred during the first function.

My Code

public XmlDocument GetXML(string[] arrInputInfo)
{
string rtnErrno=null;
ArrayList rtnValidateLogin=null;
string rtnPostSMS = null;
string strStartDateTime = DateTime.Now.ToString();
string strEndDateTime = DateTime.Now.ToString();
DataSet dst=new DataSet(); 
string[] arrLogReq = new string[5];
string strLoginRef = "-999";
string strstatus = "20";
 

 
try
{
//Validate Input Details
rtnErrno = ValidateInputInfo(arrInputInfo);
 
if (rtnErrno != "0")
{
throw new Exception("Exception raised at Input Validation");
}
//Validate Login Details
rtnValidateLogin = objLogin.ValidateCPLogin(arrInputInfo[0], arrInputInfo[1]);
 
if (rtnValidateLogin[0].ToString() != "0")
{
rtnErrno = rtnValidateLogin[0].ToString();
throw new Exception("Exception raised at Login Validation");
}
else
{
dst = (DataSet)rtnValidateLogin[1];
strLoginRef = dst.Tables[0].Rows[0][0].ToString();
}

//Insert Message
rtnPostSMS = PostSMSText(arrInputInfo);
 
if (rtnPostSMS.ToString() != "0")
{
rtnErrno = rtnPostSMS[0].ToString();
throw new Exception("Exception raised at Inserting Message");
}


}
catch (Exception ex)
{
strstatus = "30";
LogErrorHelper.LogError(Constants.ERROR_WEBSERVICES_LOGFILE, ex, "BLL.CSMSService-->GetXML");
}
finally
{
GC.Collect();
}
 

try
{
strEndDateTime = DateTime.Now.ToString();
arrLogReq[0] = strLoginRef; //Login Ref no 
arrLogReq[1] = string.Concat(arrInputInfo[2], "~", arrInputInfo[3]); //Mobile No and Message
arrLogReq[2] = strStartDateTime; //start time
arrLogReq[3] = strEndDateTime; //end time
arrLogReq[4] = strstatus;
//To Log Incoming Requests
rtnErrno = LogConfirmation(arrLogReq);
}
catch (Exception ex)
{
LogErrorHelper.LogError(Constants.ERROR_WEBSERVICES_LOGFILE, ex, "BLL.CSMSService-->GetXML");
}
finally
{
GC.Collect();
}
return GetFinalXml(rtnErrno);
}





先谢谢你...



Thanks in Advance...

推荐答案

您应该阅读(并测试代码)我的下一篇文章 MVC基本站点:第2步 - 例外管理 [ ^ ]其中详细介绍了ASP.NET MVC网站的异常管理规则及其实现,并提供了一些可以重用的日志管理规则和日志管理规则源代码(与非常小的变化)不仅在其他ASP.NET站点中,而且通常在任何.NET项目中。
You should read (and test the code from) my next article MVC Basic Site: Step 2 - Exceptions Management[^] where I present in details the exceptions management rules and their implementation for an ASP.NET MVC web site, and provides some utile base classes and source code for Logging and Exceptions Management that can be reused (with very small changes) not only in other ASP.NET sites but also generally in any .NET project.


这篇关于如何实现多个try块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆