Set VBScript Err.Number by throwing .NET exception in c# COM object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kevinst
    New Member
    • Oct 2009
    • 7

    Set VBScript Err.Number by throwing .NET exception in c# COM object

    Hello,

    I have a c# com object that exposes several methods.
    This com object is to be used in vbscripts as well as vba scripts.

    In my application error handling is done by throwing different types of custom exceptions. The problem is that when theses exceptions are caught by the vbscript the type of the exception is unknown.

    Therefore I would need Err.Number to determine what kind of exception was thrown. Unfortunately I have not been able to figure out how that works.

    For some reason ExternalExcepti on does not do the trick.

    Simple example:

    COM object:

    Code:
    [ComVisible(true)]
    public class MyClass
    {
    public void Test()
    {
    throw new MyTimeoutException() //MyTimeoutException somehow sets Err.Code to 123
    }
    }
    VBScript:

    Code:
    Dim k
    set k = CreateObject("ns.MyClass")
    on error resume next
    k.Test
    
    if err.number = 123 then
        MsgBox "Crap. Operation timed out."
    end if
    
    set k = nothing
    I have been googeling for hours without any kind of success.

    Kevin
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I would hazzard a guess that maybe you need to throw a COMException?
    I am not really sure where the error code for vbscript gets pulled from

    Comment

    • Kevinst
      New Member
      • Oct 2009
      • 7

      #3
      Thought the same thing.

      No success either.

      Comment

      • Kevinst
        New Member
        • Oct 2009
        • 7

        #4
        Solution:
        Either derive your custom exception from ApplicationExce ption and set the HResult Property or throw an exception using Marshal.ThrowEx ceptionForHR.

        Attention: The HResults have to be negative values.

        Comment

        Working...