From 2b30498147a70298f02d2429eee90d364930e548 Mon Sep 17 00:00:00 2001 From: AJ Date: Wed, 7 Apr 2021 21:06:22 -0400 Subject: [PATCH] Wrap main in try catch --- EtchBendLines/Program.cs | 51 +++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/EtchBendLines/Program.cs b/EtchBendLines/Program.cs index b8ca6b7..d234598 100644 --- a/EtchBendLines/Program.cs +++ b/EtchBendLines/Program.cs @@ -13,28 +13,51 @@ namespace EtchBendLines static void Main(string[] args) { - var path = AppDomain.CurrentDomain.BaseDirectory; - var files = Directory.GetFiles(path, "*.dxf", SearchOption.AllDirectories); - - etcher.EtchLength = double.Parse(ConfigurationManager.AppSettings["EtchLength"]); - etcher.MaxBendRadius = double.Parse(ConfigurationManager.AppSettings["MaxBendRadius"]); - - if (files == null || files.Length == 0) + try { - Console.WriteLine($"No DXF files founds. Place DXF files in \"{AppDomain.CurrentDomain.BaseDirectory}\" and run this program again."); - PressAnyKeyToExit(); - return; + var path = AppDomain.CurrentDomain.BaseDirectory; + var files = Directory.GetFiles(path, "*.dxf", SearchOption.AllDirectories); + + etcher.EtchLength = GetAppSettingAsDouble("EtchLength"); + etcher.MaxBendRadius = GetAppSettingAsDouble("MaxBendRadius"); + + if (files == null || files.Length == 0) + { + Console.WriteLine($"No DXF files founds. Place DXF files in \"{AppDomain.CurrentDomain.BaseDirectory}\" and run this program again."); + } + else + { + foreach (var file in files) + { + etcher.AddEtchLines(file); + Console.WriteLine(); + } + } } - - foreach (var file in files) + catch (Exception ex) { - etcher.AddEtchLines(file); - Console.WriteLine(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"An error occured: {ex.Message}"); + Console.ResetColor(); } PressAnyKeyToExit(); } + static double GetAppSettingAsDouble(string name) + { + double v; + + try + { + return double.Parse(ConfigurationManager.AppSettings[name]); + } + catch + { + throw new Exception($"Failed to convert the value of AppSetting[\"{name}\"] to double"); + } + } + static void PressAnyKeyToExit() { Console.WriteLine("Press any key to exit.");