Ivonne Aspilcueta

Ivonne Aspilcueta

  • 1.2k
  • 441
  • 1.4k

try catch continue execution

Jun 21 2024 6:59 PM

Hello,

I need help to verify my code for the tyr/catch, I want to add try/catch on the if statements to catch the errors but also to continue on error without stopping the execution. Thank you.

namespace FCCCU318SymitarPreprocessor
{
	class Program
	{
		static void Main(string[] args)
		{
			if (args.Length > 0)
			{
				string inputFilePath;
				string outputFilePath;

				string inputFilePathOnly;
				string fileToImport;

				string strNewLine;

				string reportID;
				string reportName;
				string reportDate;
				string reportTime;
				string FileName;
				string FileTypeNum;

				inputFilePath = args[0].ToString();
				outputFilePath = args[1].ToString();

				inputFilePathOnly = Path.GetDirectoryName(inputFilePath);

				string inputFile = inputFilePath;

				// Read Index.txt file
				using (StreamReader sr = new StreamReader(inputFile))
				{
					string lineData;
					lineData = sr.ReadLine();

					while (!sr.EndOfStream)
					{
						lineData = sr.ReadLine();

						if (lineData.Length > 6)
						{
							reportID = lineData.Substring(0, 6);

							var isNumeric = reportID.All(c => "0123456789".Contains(c));
							bool isError = false;
							try
							{
								//if the reportId is numeric process the line
								if (isNumeric)
								{
									//reportName is the OnBase DocType
									reportName = lineData.Substring(7, 41).Trim();
									if (reportName.Length > 1)
									{
										if (reportName.Length > 18)
										{
											if (reportName.Substring(0, 17).ToUpper() == "BATCH OUTPUT FOR ")
											{
												reportName = reportName.Substring(17);
											}
										}

										reportDate = lineData.Substring(49, 8).Trim();
										reportTime = lineData.Substring(61, 5).Trim();

										FileTypeNum = "1"; //Assumption is that all the Symitar Reports are text  
										FileName = reportID;  //Assumption is that the reportID will always be the file name without an extension

										fileToImport = inputFilePathOnly + @"\" + FileName;
										try
										{
										
											if (new FileInfo(fileToImport).Length > 0)
											{
												// reportName used for DocType and for the Report Name Keyword Type
												// DocType hard coded to Symitar Reports Temp as DocType is assigned form the Report Name in the DocType Assignment Workflow 
												strNewLine = reportID + "|" + "FCCCUReports" + "|" + reportName + "|" + reportDate + "|" + reportDate + " " + reportTime + "|" + FileTypeNum + "|" + FileName;

												using (StreamWriter sw = new StreamWriter(outputFilePath, true))
												{
													sw.WriteLine(strNewLine);
												}
											}

										}
										catch
                                        {
											isError = true;
										}
										if (isError) continue;
									}
								}
							}
							catch
                            {
								continue;
                            }
							if(isError) continue;
						}
					}
					Environment.ExitCode = 0;
				}
			}
		}
	}
}

 


Answers (1)