Optimized unneccesary try-catch block

This commit is contained in:
2023-03-29 09:25:12 +02:00
parent 6536ea3fb9
commit 226612f49c

View File

@@ -20,53 +20,42 @@ namespace Password_Manager
public static void Init() public static void Init()
{ {
try if (File.Exists(CONFIGPATH))
{ {
StreamReader sr = new StreamReader(CONFIGPATH); StreamReader sr = new StreamReader(CONFIGPATH);
string? path = null, recipient = null; string? path = null, recipient = null;
if (sr != null) while (!sr.EndOfStream)
{ {
while (!sr.EndOfStream) string[]? fields = sr.ReadLine().Split(DELIMETER);
if (fields != null)
{ {
string[]? fields = sr.ReadLine().Split(DELIMETER); if (fields[0] == PATH)
if (fields != null)
{ {
if (fields[0] == PATH) path = fields[1];
{
path = fields[1];
}
else if (fields[0] == RECIPIENT)
{
recipient = fields[1];
}
}
else //probably an empty line or something
{
continue;
} }
else if (fields[0] == RECIPIENT)
{
recipient = fields[1];
}
} }
else //probably an empty line or something
{
continue;
}
}
if (path != null && recipient != null) if (path != null && recipient != null)
{ {
Configuration = new Config(path, recipient); Configuration = new Config(path, recipient);
} }
else else
{ {
throw new InvalidConfigurationException("One or more required fileds were missing from the configuration file."); throw new InvalidConfigurationException("One or more required fileds were missing from the configuration file.");
} }
}
else
{
throw new FileNotFoundException();
}
} }
catch (FileNotFoundException) else
{ {
CreateDefaultConfig(); CreateDefaultConfig();
}
catch (IOException e)
{
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }