123 456 789 0
123 456 789 0

Reputation: 10865

Thread throws an exception in the middle of serialization?

Is it possible that for a given thread if it throws an exception that it will bubble to the Main Thread?

e.g,

Serialization code

using (Package package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite))
{
     using (var xpsDoc = new XpsDocument(package, CompressionOption.Normal))
     {
         var rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
         DocumentPaginator paginator = ((IDocumentPaginatorSource) flowDocument).DocumentPaginator;
         rsm.SaveAsXaml(paginator);
         rsm.Commit();
     }
}

Where inside the SaveAsXaml it iterates to the to the DocumentPaginatorSource's GetPage(int pageIndex).

What happens if a given thread executed the SaveAsXaml and it throws an Exception while iterating in it? Will it continue to iterate to the Main thread?

SaveAsXaml is something I don't have control over but I know that it tries to execute GetPage(int pageIndex) and complete the serialization by completing the number of pages it has to serialize.

Upvotes: 0

Views: 116

Answers (2)

jods
jods

Reputation: 4591

Maybe what you observed is that when an exception goes unhandled in .net, the runtime takes down the whole AppDomain as a safety measure.

So unhandled exception in a background thread == app crash.

This has changed slightly in .net 4.5 whereas unobserved exceptions in Task are ignored (previous behavior can be opted in).

Upvotes: 0

Brian Rasmussen
Brian Rasmussen

Reputation: 116471

No exceptions never automatically move from one thread to another. Certain APIs such as Task.Wait acts as a rendezvous point where marshalling of the exception happens, but it doesn't happen automatically.

If a thread throws an exception that isn't handled, the default behavior of the .NET runtime is to terminate the process due to an unhandled exception.

Upvotes: 1

Related Questions