Freigeben über


Exception Handling Behavior in HTML Bridge

Microsoft Silverlight will reach end of support after October 2021. Learn more.

This topic provides information about how exceptions are passed between managed code and JavaScript code in Silverlight.

NoteNote:

Silverlight for Windows Phone Silverlight for Windows Phone does not support the HTML Bridge feature.

Passing Managed Exceptions to JavaScript

The HTML Bridge feature does not support the UnhandledException event on the Silverlight application object. Instead, the exception string is formatted by the ToString method and returned to JavaScript. In advanced cases such as nested exceptions, only the top-level exception information and stack trace are returned to JavaScript.

Some browsers may wrap the exception text with additional boilerplate text. Therefore, localized applications that throw JavaScript exceptions may contain localized text that differs from the exception text raised from managed code.

Exceptions that occur during the processing of a JavaScript event handler (for example, when the click event of a button raises a managed event handler that throws an exception) are silently ignored. This mirrors the behavior of Document Object Model (DOM) events that trigger exceptions in the browser.

Passing JavaScript Exceptions to Managed Code

The HTML Bridge feature does not distinguish between different types of JavaScript exceptions or map them to specific managed exception types. Instead, JavaScript exceptions are returned to managed code as InvalidOperationException objects.

If managed code calls JavaScript code, and that JavaScript code calls managed code that throws an exception, the exception information is marshaled back to JavaScript and then discarded. The HTML Bridge does not maintain managed exception information, and all relationships to the original managed exception are lost.

Consider the following example:

  1. The managed method CarObject.EnumProperties calls the JavaScript function JS_GetColor.

  2. The JavaScript function JS_GetColor calls the managed method CarProperties.GetColor.

  3. CarProperties.GetColor raises an exception.

Under these conditions, the exception information that is thrown by CarProperties.GetColor is marshaled back to the JavaScript function JS_GetColor and then discarded. The original managed method, CarObject.EnumProperties, never receives the exception information.

If the browser supplies text for the exception message, the text is returned as the exception message. Exception text differs across browsers and may not be available to the HTML Bridge.

NoteNote:

In the Safari browser, JavaScript methods that fail because of errors may not return an exception to managed code. There is currently no workaround.