Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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...
Posted
Updated 1-Apr-14 19:21pm
v2
Comments
Sergey Alexandrovich Kryukov 2-Apr-14 1:10am    
All wrong: "if one method throws an exception, the other methods will not run." does not entail "this is the main reason I am using separate try..catch blocks". Not at all, not in any sense. Perhaps you just need to understand how exceptions work and analyze the implications of it. The problem is not really formulated.
—SA
ramki48 2-Apr-14 1:21am    
Sergey Alexandrovich Kryukov,

My problem is to log the incoming requests to my method wether it is success or a exception? I need sugetion where to place my code?
regards,
RK
Bernhard Hiller 2-Apr-14 2:17am    
The code is wrong on so many levels... e.g.
string strStartDateTime - oh no! use DateTime instead
string rtnErrno - You talk about exceptions, but use error numbers?
Throw it away, write everything again.
Bernhard Hiller 2-Apr-14 2:58am    
Do not repost:
http://www.codeproject.com/Messages/4791043/Multiple-try-blocks-in-csharp.aspx

1 solution

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.
 
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