I am a green C# programmer and I'm trying to add exception handling to an application but unsure of the correct way to achieve the result I'm looking for. I'm parsing a CSV and grabbing values that are always in the same place. If a blank or incorrectly formatted file comes through it will cause the application to crash and I want to prevent this. Instead of storing the value from the file, if an exception occurs I want to store a single whitespace character in the string variable. Here is my code:
using (StreamReader inputGetData = new StreamReader(filename.FullName))
{
//STORE DATA STRIPPED FROM MDT FILE
lotID = inputGetData.ReadLine().Split(',')[2];
exatronFile = inputGetData.ReadLine().Split(',')[2];
rofinFile = inputGetData.ReadLine().Split(',')[2];
lotQty = inputGetData.ReadLine().Split(',')[2];
serialize = inputGetData.ReadLine().Split(',')[2];
serialStart = inputGetData.ReadLine().Split(',')[2];
serialInc = inputGetData.ReadLine().Split(',')[2];
partNum = inputGetData.ReadLine().Split(',')[3];
inputGetData.ReadLine();
dieMNF = inputGetData.ReadLine().Split(',')[3];
assemblySub = inputGetData.ReadLine().Split(',')[3];
countryOrigin = inputGetData.ReadLine().Split(',')[3];
dateCode = inputGetData.ReadLine().Split(',')[3];
QMLcomp = inputGetData.ReadLine().Split(',')[3];
prefix = inputGetData.ReadLine().Split(',')[3];
serialNum = inputGetData.ReadLine().Split(',')[3];
suffix = inputGetData.ReadLine().Split(',')[3];
inputGetData.ReadLine();
packageCode = inputGetData.ReadLine().Split(',')[1];
So ideally I want to try each line, catch an exception if it occurs, throw it to the higher method for logging and then store ' ' into the variable. If I put that last part into the finally block it will always run even if an exception doesn't occur correct? How can I achieve this?