Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:54:39+00:00 2026-06-10T08:54:39+00:00

I’m having trouble adding proper exception handling to existing code that makes heavy use

  • 0

I’m having trouble adding proper exception handling to existing code that makes heavy use of Silverlight – JavaScript interoperability. In this case, my JavaScript can throw an exception that I want to handle meaningfully in Silverlight.

From Silverlight, I’m creating an instance of a JavaScript object, then later I’m calling a method on that object:

public class MyWrapper
{
    dynamic _myJSObject;

    public MyWrapper()
    {
        _myJSObject = HtmlPage.Window.CreateInstance("MyJSObject");
    }

    public int MyMethod()
    {
        try
        {
            int result = (int)_myJSObject.MyMethod();
        }
        catch (Exception ex)
        {
            // I want to add meaningful exception handling here
        }      
    }
}

Whenever MyJSObject.MyMethod throws an exception, there are two problems:

  • The browser shows a message that an exception has occurred.
  • Information about the exception is not passed to my managed code. Instead I get a RuntimeBinderException which just says “Cannot invoke a non-delegate type” and contains no other information whatsoever. This does not seem to match what is described here; I’d expect an InvalidOperationException.

I’ve tried avoiding to cast the returned value of the method:

object tmp= _myJSObject.MyMethod();

This makes no difference. Changing the type of exception thrown on the JavaScript side has no effect either.

MyJSObject.prototype.MyMethod = function ()
                                {
                                    throw "Hello Silverlight!";
                                }

The only solution I can think of right now is abusing the function’s return value to pass information about the exception, but that will make my code a whole lot uglier… so:

Why is the behavior I’m seeing different from what is described in documentation? Does it have to do with my use of dynamic somehow? How can I properly handle exceptions that occur in JavaScript in my managed code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:54:41+00:00Added an answer on June 10, 2026 at 8:54 am

    After quite a bit of experimentation, I concluded that there is no way to directly handle the JavaScript exception from Silverlight. In order to be able to process the exception, the JavaScript code needs to be changed slightly.

    Instead of throwing the error, I return it:

    function MyMethod()
    {
        try
        {
            // Possible exception here
        }
        catch (ex)
        {
            return new Error(ex);
        }
    }
    

    Then on the Silverlight side, I use a wrapper around ScriptObject to turn the return value into an exception again. The key here is the TryInvokeMember method:

    public class ScriptObjectWrapper : DynamicObject
    {
        private ScriptObject _scriptObject;
    
        public ScriptObjectWrapper(ScriptObject scriptObject)
        {
            _scriptObject = scriptObject;
        }
    
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            result = _scriptObject.Invoke(binder.Name, args);
    
            ScriptObject s = result as ScriptObject;
            if (s != null)
            {
                // The JavaScript Error object defines name and message properties.
                string name = s.GetProperty("name") as string;
                string message = s.GetProperty("message") as string;
                if (name != null && message != null && name.EndsWith("Error"))
                {
                    // Customize this to throw a more specific exception type
                    // that also exposed the name property.
                    throw new Exception(message);
                }
            }
    
            return true;
        }
    
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            try
            {
                _scriptObject.SetProperty(binder.Name, value);
                return true;
            }
            catch
            {
                return false;
            }
        }
    
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            try
            {
                result = _scriptObject.GetProperty(binder.Name);
                return true;
            }
            catch
            {
                result = null;
                return false;
            }
        }
    
    }
    

    Potentially you could improve this wrapper so it actually injects the JavaScript try-catch mechanism transparently, however in my case I had direct control over the JavaScript source code, so there was no need to do this.

    Instead of using the built in JavaScript Error object, it’s possible to use your custom objects, as long as the name property ends with Error.

    To use the wrapper, the original code would change to:

    public MyWrapper()
    {
        _myJSObject = new ScriptObjectWrapper(
                      HtmlPage.Window.CreateInstance("MyJSObject"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.