Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 075df17

Browse files
MarcoRossignolishmao
authored andcommitted
"Don't directly throw Exception" System.Runtime.Serialization.Json (#25796)
* Don't directly throw Exception
1 parent cd3bcb8 commit 075df17

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlJsonReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ protected override void Dispose(bool disposing)
354354
throw;
355355
}
356356

357-
throw new Exception(SR.GenericCallbackException, e);
357+
throw new InvalidOperationException(SR.GenericCallbackException, e);
358358
}
359359
}
360360
base.Dispose(disposing);

src/System.Runtime.Serialization.Json/tests/DataContractJsonSerializer.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2931,7 +2931,34 @@ public static void DCJS_ValidateExceptionOnUnspecifiedRootSerializationType()
29312931

29322932
Assert.Equal(value.MyIntProperty, actual.MyIntProperty);
29332933
Assert.Equal(value.MyStringProperty, actual.MyStringProperty);
2934-
}
2934+
}
2935+
2936+
[Fact]
2937+
public static void DSJS_ThrowExceptionOnDispose()
2938+
{
2939+
using (MemoryStream ms = new MemoryStream(System.Text.Encoding.Unicode.GetBytes("{}")))
2940+
{
2941+
XmlDictionaryReader jsonReader = JsonReaderWriterFactory.CreateJsonReader(ms, System.Text.Encoding.Unicode, XmlDictionaryReaderQuotas.Max,
2942+
reader =>
2943+
{
2944+
//sample exception on reader close
2945+
throw new DivideByZeroException();
2946+
});
2947+
try
2948+
{
2949+
jsonReader.Dispose();
2950+
Assert.False(true);
2951+
}
2952+
catch (Exception ex)
2953+
{
2954+
Assert.True(
2955+
ex is InvalidOperationException ||
2956+
//Netfx throws System.Runtime.CallbackException
2957+
ex.GetType().FullName == "System.Runtime.CallbackException"
2958+
);
2959+
}
2960+
}
2961+
}
29352962

29362963
private static T SerializeAndDeserialize<T>(T value, string baseline, DataContractJsonSerializerSettings settings = null, Func<DataContractJsonSerializer> serializerFactory = null, bool skipStringCompare = false)
29372964
{

0 commit comments

Comments
 (0)