From 226612f49c53532fda9e90253a3208115a02ffbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miskolczi=20Rich=C3=A1rd?= Date: Wed, 29 Mar 2023 09:25:12 +0200 Subject: [PATCH] Optimized unneccesary try-catch block --- Password Manager/ConfigFileManager.cs | 61 +++++++++++---------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/Password Manager/ConfigFileManager.cs b/Password Manager/ConfigFileManager.cs index 6d0fa58..c19394b 100644 --- a/Password Manager/ConfigFileManager.cs +++ b/Password Manager/ConfigFileManager.cs @@ -20,53 +20,42 @@ namespace Password_Manager public static void Init() { - try + if (File.Exists(CONFIGPATH)) { StreamReader sr = new StreamReader(CONFIGPATH); - string? path = null, recipient = null; - if (sr != null) + string? path = null, recipient = null; + while (!sr.EndOfStream) { - while (!sr.EndOfStream) + string[]? fields = sr.ReadLine().Split(DELIMETER); + if (fields != null) { - string[]? fields = sr.ReadLine().Split(DELIMETER); - if (fields != null) + if (fields[0] == PATH) { - if (fields[0] == PATH) - { - path = fields[1]; - } - else if (fields[0] == RECIPIENT) - { - recipient = fields[1]; - } - } - else //probably an empty line or something - { - continue; + path = fields[1]; } + else if (fields[0] == RECIPIENT) + { + recipient = fields[1]; + } } + else //probably an empty line or something + { + continue; + } + } - if (path != null && recipient != null) - { - Configuration = new Config(path, recipient); - } - else - { - throw new InvalidConfigurationException("One or more required fileds were missing from the configuration file."); - } - } - else - { - throw new FileNotFoundException(); - } + if (path != null && recipient != null) + { + Configuration = new Config(path, recipient); + } + else + { + throw new InvalidConfigurationException("One or more required fileds were missing from the configuration file."); + } } - catch (FileNotFoundException) + else { CreateDefaultConfig(); - } - catch (IOException e) - { - MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }