Skip to content

Commit 31c6a59

Browse files
committed
Fix issue 156: unhandled exception on drag/drop text in ill-formatted numeric field.
1 parent d1e8aec commit 31c6a59

File tree

8 files changed

+92
-52
lines changed

8 files changed

+92
-52
lines changed

Source/WPF/MoneyPackage/Package.appxmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap rescap">
3-
<Identity Name="43906ChrisLovett.MyMoney.Net" Publisher="CN=Chris Lovett, O=Chris Lovett, S=Washington, C=US" Version="2.1.0.51" />
3+
<Identity Name="43906ChrisLovett.MyMoney.Net" Publisher="CN=Chris Lovett, O=Chris Lovett, S=Washington, C=US" Version="2.1.0.52" />
44
<Properties>
55
<DisplayName>MyMoney.Net</DisplayName>
66
<PublisherDisplayName>Chris Lovett</PublisherDisplayName>

Source/WPF/MyMoney/App.xaml.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Walkabout.Configuration;
1111
using Walkabout.Help;
1212
using Walkabout.Utilities;
13+
using Walkabout.WpfConverters;
1314

1415
#if PerformanceBlocks
1516
using Microsoft.VisualStudio.Diagnostics.PerformanceProvider;
@@ -314,6 +315,14 @@ private void TaskScheduler_UnobservedTaskException1(object sender, UnobservedTas
314315
public bool HandleUnhandledException(object exceptionObject)
315316
{
316317
Exception ex = exceptionObject as Exception;
318+
if (ex is ValueConverterException)
319+
{
320+
// This exception can be raised if you drag/drop text in a numeric field that results in an invalid format.
321+
// We cannot catch this exception because there is no Money code on the stack except the originator of the
322+
// exception in WpfConverters.cs. Fixes issue #156.
323+
return true;
324+
}
325+
317326
string message = null;
318327
string details = null;
319328
if (ex == null && exceptionObject != null)

Source/WPF/MyMoney/Controls/WpfConverters.cs

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
namespace Walkabout.WpfConverters
1414
{
15+
public class ValueConverterException : Exception
16+
{
17+
public ValueConverterException(string message) : base(message) { }
18+
}
19+
1520
// Extracts the first letter of the Category name.
1621
public class CategoryTypeLetterConverter : IValueConverter
1722
{
@@ -478,43 +483,51 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
478483
{
479484
if (value != null && value.GetType() != typeof(string))
480485
{
481-
throw new Exception("Unexpected value type passed to PreserveDecimalDigitsValueConverter.ConvertBack : " + value.GetType().Name);
486+
throw new ValueConverterException("Unexpected value type passed to PreserveDecimalDigitsValueConverter.ConvertBack : " + value.GetType().Name);
482487
}
483-
string s = (string)value;
484-
485-
if (targetType == typeof(SqlDecimal))
488+
try
486489
{
487-
if (string.IsNullOrWhiteSpace(s))
490+
string s = (string)value;
491+
492+
if (targetType == typeof(SqlDecimal))
488493
{
489-
return new SqlDecimal();
494+
if (string.IsNullOrWhiteSpace(s))
495+
{
496+
return new SqlDecimal();
497+
}
498+
return SqlDecimal.Parse(s);
490499
}
491-
return SqlDecimal.Parse(s);
492-
}
493-
else if (targetType == typeof(decimal))
494-
{
495-
if (string.IsNullOrWhiteSpace(s))
500+
else if (targetType == typeof(decimal))
496501
{
497-
return 0D;
502+
if (string.IsNullOrWhiteSpace(s))
503+
{
504+
return 0D;
505+
}
506+
507+
return decimal.Parse(s);
498508
}
509+
else if (targetType == typeof(DateTime))
510+
{
511+
if (string.IsNullOrWhiteSpace(s))
512+
{
513+
return DateTime.Now;
514+
}
499515

500-
return decimal.Parse(s);
501-
}
502-
else if (targetType == typeof(DateTime))
503-
{
504-
if (string.IsNullOrWhiteSpace(s))
516+
return DateTime.Parse(s);
517+
}
518+
else if (targetType == typeof(string))
505519
{
506-
return DateTime.Now;
520+
return s;
521+
}
522+
else
523+
{
524+
throw new Exception("Unexpected target type passed to PreserveDecimalDigitsValueConverter.ConvertBack : " + value.GetType().Name);
507525
}
508-
509-
return DateTime.Parse(s);
510-
}
511-
else if (targetType == typeof(string))
512-
{
513-
return s;
514526
}
515-
else
527+
catch (Exception ex)
516528
{
517-
throw new Exception("Unexpected target type passed to PreserveDecimalDigitsValueConverter.ConvertBack : " + value.GetType().Name);
529+
// Need to wrap it in something we can recognize later in HandleUnhandledException
530+
throw new ValueConverterException(ex.Message);
518531
}
519532
}
520533
}
@@ -537,19 +550,27 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
537550

538551
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
539552
{
540-
if (value is string)
553+
try
541554
{
542-
string s = (string)value;
543-
if (string.IsNullOrWhiteSpace(s))
555+
if (value is string)
544556
{
545-
return SqlDecimal.Null;
557+
string s = (string)value;
558+
if (string.IsNullOrWhiteSpace(s))
559+
{
560+
return SqlDecimal.Null;
561+
}
562+
return new SqlDecimal(System.Convert.ToDecimal(value));
546563
}
547-
return new SqlDecimal(System.Convert.ToDecimal(value));
548-
}
549564

550-
if (value is decimal)
565+
if (value is decimal)
566+
{
567+
return new SqlDecimal((decimal)value);
568+
}
569+
}
570+
catch (Exception ex)
551571
{
552-
return new SqlDecimal((decimal)value);
572+
// Need to wrap it in something we can recognize later in HandleUnhandledException
573+
throw new ValueConverterException(ex.Message);
553574
}
554575
return new SqlDecimal(0);
555576
}
@@ -571,19 +592,26 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
571592

572593
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
573594
{
574-
575-
if (value is string)
595+
try
576596
{
577-
string stringVal = value as string;
578-
579-
decimal d = 0;
580-
if (!string.IsNullOrWhiteSpace(stringVal) &&
581-
false == decimal.TryParse(stringVal, NumberStyles.Currency, CultureInfo.CurrentCulture, out d))
597+
if (value is string)
582598
{
583-
d = System.Convert.ToDecimal(stringVal, CultureInfo.GetCultureInfo("en-US"));
584-
}
599+
string stringVal = value as string;
600+
601+
decimal d = 0;
602+
if (!string.IsNullOrWhiteSpace(stringVal) &&
603+
false == decimal.TryParse(stringVal, NumberStyles.Currency, CultureInfo.CurrentCulture, out d))
604+
{
605+
d = System.Convert.ToDecimal(stringVal, CultureInfo.GetCultureInfo("en-US"));
606+
}
585607

586-
return d;
608+
return d;
609+
}
610+
}
611+
catch (Exception ex)
612+
{
613+
// Need to wrap it in something we can recognize later in HandleUnhandledException
614+
throw new ValueConverterException(ex.Message);
587615
}
588616
return value;
589617
}

Source/WPF/MyMoney/Properties/PublishProfiles/ClickOnceProfile.pubxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
44
-->
55
<Project>
66
<PropertyGroup>
7-
<ApplicationRevision>51</ApplicationRevision>
8-
<ApplicationVersion>2.1.0.51</ApplicationVersion>
7+
<ApplicationRevision>52</ApplicationRevision>
8+
<ApplicationVersion>2.1.0.52</ApplicationVersion>
99
<BootstrapperEnabled>True</BootstrapperEnabled>
1010
<Configuration>Release</Configuration>
1111
<CreateWebPageOnPublish>True</CreateWebPageOnPublish>

Source/WPF/MyMoney/Setup/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<changes>
3+
<change version="2.1.0.2" date="9/19/2025">
4+
- Bug: Fix issue #156: unhandled exception on drag/drop text in ill-formatted numeric field.
5+
</change>
36
<change version="2.1.0.51" date="5/11/2025">
47
- Bug: Fix crash during startup if %LOCALAPPDATA%\MyMoney\tempfiles.xml got corrupted somehow.
58
</change>

Source/WPF/Version/Version.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// And also sync this with the MSIX package manifest in
77
// ~\MyMoney.Net\Source\WPF\MoneyPackage\Package.appxmanifest
88

9-
[assembly: AssemblyVersion("2.1.0.51")]
10-
[assembly: AssemblyFileVersion("2.1.0.51")]
9+
[assembly: AssemblyVersion("2.1.0.52")]
10+
[assembly: AssemblyFileVersion("2.1.0.52")]

Source/WPF/Version/Version.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<ApplicationRevision>51</ApplicationRevision>
5-
<ApplicationVersion>2.1.0.51</ApplicationVersion>
4+
<ApplicationRevision>52</ApplicationRevision>
5+
<ApplicationVersion>2.1.0.52</ApplicationVersion>
66
</PropertyGroup>
77
</Project>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.0.51
1+
2.1.0.52

0 commit comments

Comments
 (0)