locked
Catching Exception from a winmd component

    Question

  • Any idea how I could catch a exception in Metro javascript??  That is, if my Task method gets an exception, I'm doing a TrySetException() on the TaskCompletionSource.

    How would I actually respond to that in javascript?  I've tried doing a exception outside and inside the javascript function().

    My TrySetResult works fine. When doing a TrySetException, I'm never seeing my call back.

     

    Thanks,

    -Greg

    Tuesday, November 15, 2011 1:23 AM

Answers

  • Greg,

    Assuming your class is in a C# WinMD component... You should throw the exception in your C# WinMD object like this:

      public sealed class ToUpper
        {
            public string talkBack(string theVal)
            {
                throw new Exception("Here is a message");
               
                return theVal.ToUpper();
            }
        }

    Then you can catch the exception in jscript like this:

     function btnClick() {
            try{
                var myWinmd = new SayItBack.ToUpper();
                resultTxt.innerHTML= myWinmd.talkBack("clicked");
            } catch (ex) {
               
                resultTxt.innerHTML = ex.message;
            }

    }

    -Jeff


    Jeff Sanders (MSFT)
    • Marked as answer by Bob_Bao Wednesday, December 07, 2011 7:36 AM
    Wednesday, November 30, 2011 3:21 PM
    Moderator

All replies

  • Any idea how I could catch a exception in Metro javascript??  That is, if my Task method gets an exception, I'm doing a TrySetException() on the TaskCompletionSource.

    How would I actually respond to that in javascript?  I've tried doing a exception outside and inside the javascript function().

    My TrySetResult works fine. When doing a TrySetException, I'm never seeing my call back.

     

    Thanks,

    -Greg


    There must be some kind of rule - right after asking a question on-line, you figure it out. 

    The deal is you need another then, with the correct signature.  SetResult will return one param.  SetException will return two.

     

      MyClass.MyAsynCal(value).then(        
                function (json) {            
                    showMessage(json);         
                }, function (a, b) {
                    showError(a.message);
                }
    

    The function(a,b) above will get called when SetException is called.

    My NEXT question is how to get the actual exception!  The above has "Unknown runtime error" in a.message.  The exception is fully formed in the WinRT piece.  b is undefined.  Any ideas?

    Tuesday, November 15, 2011 1:37 AM
  • Greg,

    Assuming your class is in a C# WinMD component... You should throw the exception in your C# WinMD object like this:

      public sealed class ToUpper
        {
            public string talkBack(string theVal)
            {
                throw new Exception("Here is a message");
               
                return theVal.ToUpper();
            }
        }

    Then you can catch the exception in jscript like this:

     function btnClick() {
            try{
                var myWinmd = new SayItBack.ToUpper();
                resultTxt.innerHTML= myWinmd.talkBack("clicked");
            } catch (ex) {
               
                resultTxt.innerHTML = ex.message;
            }

    }

    -Jeff


    Jeff Sanders (MSFT)
    • Marked as answer by Bob_Bao Wednesday, December 07, 2011 7:36 AM
    Wednesday, November 30, 2011 3:21 PM
    Moderator