exception handlingI am a newbie writing code for my c# class. We have to display exception handling but I ran into a red squiggly on my "lines" variable in my for each loop because I put a try catch around the method i declared lines in. How do I fix this? Thanks in advance!
I tried looking for similar issues on other forum with no luck.
>Solution :
You can declare the variable lines before try block, like this:
string[] lines = new string[0];
try {
lines = File.ReadAllText(txtFile);
}
catch (Exception e) {
//....
}
//...
foreach (var line in lines) {
//...
}