Link to home
Start Free TrialLog in
Avatar of akohan
akohan

asked on

question on C# and exception



Hello group,

I'm reading some input file (text) where I have to parse and tokenzie some data. In order to extract a field such as
applicant-id I have used:

       trans.applicant_id = arData[x].Substring(0, 10);
        
which works fine as long as there is a valid value for applicant id in the text file like:

application id 0123456789

however, if I receive file without the value then I will run into exception error since I have put the code in a try/catch block.

What should I do (using if statement) in order not to get into exception when value doesn't exist.

Regards
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

you mean:
if (arData[x] != string.Empty)
{
       trans.applicant_id = arData[x].Substring(0, 10);
} 

or do you want to tryparse to numerical data type?

Open in new window

try this
if(arData[x].Length >= 10)
{
    trans.applicant_id = arData[x].Substring(0, 10);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Hamid Hassan
Hamid Hassan
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial