From 277097d9f65acd5c5cafbe39d76929234d60bf48 Mon Sep 17 00:00:00 2001 From: Frahman <152763742+firahman@users.noreply.github.com> Date: Sun, 3 Dec 2023 00:18:23 -0500 Subject: [PATCH] Update EquationViewModel.cpp error handling Catching unhandled exceptions instead of crashing --- .../GraphingCalculator/EquationViewModel.cpp | 84 ++++++++++--------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/src/CalcViewModel/GraphingCalculator/EquationViewModel.cpp b/src/CalcViewModel/GraphingCalculator/EquationViewModel.cpp index e7829c757..f2c16893b 100644 --- a/src/CalcViewModel/GraphingCalculator/EquationViewModel.cpp +++ b/src/CalcViewModel/GraphingCalculator/EquationViewModel.cpp @@ -52,48 +52,56 @@ namespace CalculatorApp::ViewModel void EquationViewModel::PopulateKeyGraphFeatures(KeyGraphFeaturesInfo ^ graphEquation) { - if (graphEquation->AnalysisError != 0) + try { - AnalysisErrorVisible = true; - if (graphEquation->AnalysisError == static_cast(AnalysisErrorType::AnalysisCouldNotBePerformed)) - { - AnalysisErrorString = m_resourceLoader->GetString(L"KGFAnalysisCouldNotBePerformed"); - } - else if (graphEquation->AnalysisError == static_cast(AnalysisErrorType::AnalysisNotSupported)) - { - AnalysisErrorString = m_resourceLoader->GetString(L"KGFAnalysisNotSupported"); - } - else if (graphEquation->AnalysisError == static_cast(AnalysisErrorType::VariableIsNotX)) + if (graphEquation->AnalysisError != 0) { - AnalysisErrorString = m_resourceLoader->GetString(L"KGFVariableIsNotX"); + AnalysisErrorVisible = true; + if (graphEquation->AnalysisError == static_cast(AnalysisErrorType::AnalysisCouldNotBePerformed)) + { + AnalysisErrorString = m_resourceLoader->GetString(L"KGFAnalysisCouldNotBePerformed"); + } + else if (graphEquation->AnalysisError == static_cast(AnalysisErrorType::AnalysisNotSupported)) + { + AnalysisErrorString = m_resourceLoader->GetString(L"KGFAnalysisNotSupported"); + } + else if (graphEquation->AnalysisError == static_cast(AnalysisErrorType::VariableIsNotX)) + { + AnalysisErrorString = m_resourceLoader->GetString(L"KGFVariableIsNotX"); + } + return; } - return; + + KeyGraphFeaturesItems->Clear(); + + AddKeyGraphFeature(m_resourceLoader->GetString(L"Domain"), graphEquation->Domain, m_resourceLoader->GetString(L"KGFDomainNone")); + AddKeyGraphFeature(m_resourceLoader->GetString(L"Range"), graphEquation->Range, m_resourceLoader->GetString(L"KGFRangeNone")); + AddKeyGraphFeature(m_resourceLoader->GetString(L"XIntercept"), graphEquation->XIntercept, m_resourceLoader->GetString(L"KGFXInterceptNone")); + AddKeyGraphFeature(m_resourceLoader->GetString(L"YIntercept"), graphEquation->YIntercept, m_resourceLoader->GetString(L"KGFYInterceptNone")); + AddKeyGraphFeature(m_resourceLoader->GetString(L"Minima"), graphEquation->Minima, m_resourceLoader->GetString(L"KGFMinimaNone")); + AddKeyGraphFeature(m_resourceLoader->GetString(L"Maxima"), graphEquation->Maxima, m_resourceLoader->GetString(L"KGFMaximaNone")); + AddKeyGraphFeature( + m_resourceLoader->GetString(L"InflectionPoints"), graphEquation->InflectionPoints, m_resourceLoader->GetString(L"KGFInflectionPointsNone")); + AddKeyGraphFeature( + m_resourceLoader->GetString(L"VerticalAsymptotes"), graphEquation->VerticalAsymptotes, m_resourceLoader->GetString(L"KGFVerticalAsymptotesNone")); + AddKeyGraphFeature( + m_resourceLoader->GetString(L"HorizontalAsymptotes"), + graphEquation->HorizontalAsymptotes, + m_resourceLoader->GetString(L"KGFHorizontalAsymptotesNone")); + AddKeyGraphFeature( + m_resourceLoader->GetString(L"ObliqueAsymptotes"), graphEquation->ObliqueAsymptotes, m_resourceLoader->GetString(L"KGFObliqueAsymptotesNone")); + AddParityKeyGraphFeature(graphEquation); + AddPeriodicityKeyGraphFeature(graphEquation); + AddMonotoncityKeyGraphFeature(graphEquation); + AddTooComplexKeyGraphFeature(graphEquation); + + AnalysisErrorVisible = false; } - - KeyGraphFeaturesItems->Clear(); - - AddKeyGraphFeature(m_resourceLoader->GetString(L"Domain"), graphEquation->Domain, m_resourceLoader->GetString(L"KGFDomainNone")); - AddKeyGraphFeature(m_resourceLoader->GetString(L"Range"), graphEquation->Range, m_resourceLoader->GetString(L"KGFRangeNone")); - AddKeyGraphFeature(m_resourceLoader->GetString(L"XIntercept"), graphEquation->XIntercept, m_resourceLoader->GetString(L"KGFXInterceptNone")); - AddKeyGraphFeature(m_resourceLoader->GetString(L"YIntercept"), graphEquation->YIntercept, m_resourceLoader->GetString(L"KGFYInterceptNone")); - AddKeyGraphFeature(m_resourceLoader->GetString(L"Minima"), graphEquation->Minima, m_resourceLoader->GetString(L"KGFMinimaNone")); - AddKeyGraphFeature(m_resourceLoader->GetString(L"Maxima"), graphEquation->Maxima, m_resourceLoader->GetString(L"KGFMaximaNone")); - AddKeyGraphFeature( - m_resourceLoader->GetString(L"InflectionPoints"), graphEquation->InflectionPoints, m_resourceLoader->GetString(L"KGFInflectionPointsNone")); - AddKeyGraphFeature( - m_resourceLoader->GetString(L"VerticalAsymptotes"), graphEquation->VerticalAsymptotes, m_resourceLoader->GetString(L"KGFVerticalAsymptotesNone")); - AddKeyGraphFeature( - m_resourceLoader->GetString(L"HorizontalAsymptotes"), - graphEquation->HorizontalAsymptotes, - m_resourceLoader->GetString(L"KGFHorizontalAsymptotesNone")); - AddKeyGraphFeature( - m_resourceLoader->GetString(L"ObliqueAsymptotes"), graphEquation->ObliqueAsymptotes, m_resourceLoader->GetString(L"KGFObliqueAsymptotesNone")); - AddParityKeyGraphFeature(graphEquation); - AddPeriodicityKeyGraphFeature(graphEquation); - AddMonotoncityKeyGraphFeature(graphEquation); - AddTooComplexKeyGraphFeature(graphEquation); - - AnalysisErrorVisible = false; + catch (Exception^ ex) + { + AnalysisErrorVisible = true; + AnalysisErrorString = "Invalid input: " + ex->Message; + } } void EquationViewModel::AddKeyGraphFeature(String ^ title, String ^ expression, String ^ errorString)