Throwing exceptions and passing exception message on to excel

774 views
Skip to first unread message

david.a...@googlemail.com

unread,
Nov 7, 2008, 4:33:42 AM11/7/08
to ExcelDna
Hi,

First of all, many thanks for making ExcelDna! It really is a godsend!

I have a query regarding the throwing of exceptions in c# and passing
the exception message on to excel. I am currently writing a set of
UDFs in c# to enable the checks to be carried out following a codified
procedure. The syntax I use in most of my functions is as follows:

public object SomeFunction(double Arg1, int Arg2, ...)
{
try
{
Some code to carry out checks with if statements to throw
exceptions like this:
if (Arg1 == 0)
throw new CustomException.CustomExceptionType1();
More code carried out if Arg1 is non-zero.
}
catch(Exception e)
{
throw new CustomException.CustomExceptionType1();
}
}

I have a bunch of custom exceptions defined in earlier in my class
which cover various cases in which the functions can not be evaluated
using the codified guidelines. The main point of those custom
exceptions is that the exception message is supposed to give a helpful
message to the user in excel as to why the function could not be
evaluated.

I know that I could just return a message instead of throwing an
exception, but I call some of my functions and returning a string
instead of an integer causes an exception in the calling function,
whereas by throwing an exception in the called function I can catch it
via the try-catch construction in the calling function.

However, when one of my custom exceptions is thrown at any level
whilst using the UDFs in excel all I get is a #NUM error, or nothing
at all.
Is there some way to return the message of the exception which has
been thrown instead?

Luke Jackson

unread,
Nov 8, 2008, 4:51:15 AM11/8/08
to exce...@googlegroups.com
Hi David

I'm not sure there is a way to pass error messages back to Excel other than
by setting the return value. However, you can easily show a message dialog
from within your plug in code showing the error message, using
MessageBox.Show() from Windows Forms.

Luke

Govert van Drimmelen

unread,
Nov 10, 2008, 4:04:22 AM11/10/08
to ExcelDna
Hi David,

As you point out, the only way you can currently put in place custom
exception handling is by creating UDFs that wrap the calls to your
functions in an exception handler, and translate the caught exceptions
to strings before returning a result to Excel.
ExcelDna is designed so that you could automate the generation of
these wrappers in your own library, without changing the ExcelDna
libraries. You would do this by examining your libraries using
reflection and generating dynamic methods that contain the exception
handlers. An example of generating a dynamic method and exporting this
method to Excel is included in the ExcelDna distribution in the
Samples08.dna file.

Perhaps a better way to deal with this in future would be for the
ExcelDna library to allow a custom exception processing method to be
declared or registered, and have the top-level exception handler call
this user-defined handler. The default behaviour would still be the
current handler which just returns the #VALUE error value to Excel.
However, adding this would not be on top of my to-do list, unless I
get some feedback suggesting that many users would like to see this.

Regards,
Govert

On Nov 7, 11:33 am, "david.allga...@googlemail.com"

david.a...@googlemail.com

unread,
Nov 13, 2008, 11:00:53 AM11/13/08
to ExcelDna
Hi Govert and Luke,

Thanks for your suggestions. I have to say that although I get the
principle of what Govert is suggesting, I have no idea how to
implement it yet, but one of my colleagues who has a background in
programming is going to have a look into it in more detail for me.
Luke, your suggestion is good, but I reckon that would involve having
to click through hundreds of potential popups every time a computation
runs, which might not be ideal.
At least it's is throwing out a #NUM error when an exception is thrown
somewhere along the chain of functions, which is better than coming up
with a wrong value.
Thanks very much for your suggestions!

David
Reply all
Reply to author
Forward
0 new messages