C#C
C#4y ago
big OOF

Exception vs Logic

Hello, im going to read a file with File.Readlines().

I read that its better to use logic(like a if-statement) rather then try/catch.

Which of these two would you say is better practice?
Option 1.
try{
fileLines = File.ReadAllLines(fileName);
}catch { throw; }


Option 2.
if (File.Exists(fileName))
{
try
{
fileLines = File.ReadLines(fileName);
}
catch { throw; }
}else { throw new FileNotFoundException(); }

Im thinking option 1 will throw filenotfoundexception either way, and therefore its no reason to check it like in option 2?

Thanks in advance!
Was this page helpful?