473,509 Members | 3,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

7 New Member
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 ExternalException does not do the trick.

Simple example:

COM object:

Expand|Select|Wrap|Line Numbers
  1. [ComVisible(true)]
  2. public class MyClass
  3. {
  4. public void Test()
  5. {
  6. throw new MyTimeoutException() //MyTimeoutException somehow sets Err.Code to 123
  7. }
  8. }
  9.  
vbscript:

Expand|Select|Wrap|Line Numbers
  1. Dim k
  2. set k = CreateObject("ns.MyClass")
  3. on error resume next
  4. k.Test
  5.  
  6. if err.number = 123 then
  7.     MsgBox "Crap. Operation timed out."
  8. end if
  9.  
  10. set k = nothing
  11.  
I have been googeling for hours without any kind of success.

Kevin
Dec 14 '09 #1
3 3414
Plater
7,872 Recognized Expert Expert
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
Dec 14 '09 #2
Kevinst
7 New Member
Thought the same thing.

No success either.
Dec 14 '09 #3
Kevinst
7 New Member
Solution:
Either derive your custom exception from ApplicationException and set the HResult Property or throw an exception using Marshal.ThrowExceptionForHR.

Attention: The HResults have to be negative values.
Dec 15 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
5260
by: Tash Robinson | last post by:
Hi I am kind of new to active-x programming, and need a point in the right direction. I have an active-x control that I wrote in VB6. I wrote a testbed in VB and everything seems to work OK....
16
9312
by: Mike Schinkel | last post by:
Does anyone know if there are bugs in VBScript's GetRef()? I'm using VBScript Version 5.6.8515 on Win2003Server w/ASP. Sometimes it returns an object that VarType() says is a vbObject. Other...
2
5370
by: Pippen | last post by:
Below is a code snippet that is throwing the following error: Error Source: Microsoft DTS Package Error Description: Error Code: 0 Error Source= Microsoft VBScript runtime error Error...
2
13416
by: Ken Layton | last post by:
Attempting to pass a VBScript Array of Bytes to a C# Managed COM object has resulted in some very undesirable and strange behavior. When definining a method in my C# COM class as "void Write(byte...
21
4374
by: mihai | last post by:
People say that is a bad technique to throw exception from constructors; and that the solution would be to create a function _create_ to initialize an object. What about copy constructors? How...
40
13468
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
0
1322
by: Dan Sikorsky | last post by:
The event SomeEvent() is raised and handled in VB6 fine. Using this exact same code and using VBScript with the CreateObject call causes an error in the c# code. When using VBScript, I get a...
40
36046
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
21
1690
by: Chris M. Thomasson | last post by:
Is it every appropriate to throw in a dtor? I am thinking about a simple example of a wrapper around a POSIX file... ________________________________________________________________________ class...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.