471,338 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Create Exception object

I am calling a function that requires an Exception which you normally get
from a Catch ( Catch(Exception exc)).

But I have a message I am trying to pass to this funtion, but since it
requires an Exception object, I can't just send the message.

I need to create an Exception object and make the exc.Message field equal to
my message and then pass the new object to the function.

Is there a way to do this other than forcing a Catch?

Thanks,

Tom
Jun 27 '08 #1
3 1761
tshad wrote:
I am calling a function that requires an Exception which you normally get
from a Catch ( Catch(Exception exc)).
So this function is like:
public void someFunc(Exception e)
?
But I have a message I am trying to pass to this funtion, but since it
requires an Exception object, I can't just send the message.

I need to create an Exception object and make the exc.Message field equal to
my message and then pass the new object to the function.

Is there a way to do this other than forcing a Catch?
someFunc(new Exception("Message"));

Unless I'm misunderstanding what you mean.
Some simple sample code would go a long way towards clearing up any confusion.
Chris.
Jun 27 '08 #2
As Chris indicated, Exception is a class just like any other class. Simply
create a new instance of Exception and set whatever properties you want,
then pass it into the method call as the parameter, ex:

Exception ex = new Exception();
ex.Message="blah, blah";
MyMethod (ex);

--peter
"tshad" <tf*@dslextreme.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I am calling a function that requires an Exception which you normally get
from a Catch ( Catch(Exception exc)).

But I have a message I am trying to pass to this funtion, but since it
requires an Exception object, I can't just send the message.

I need to create an Exception object and make the exc.Message field equal
to my message and then pass the new object to the function.

Is there a way to do this other than forcing a Catch?

Thanks,

Tom
Jun 27 '08 #3
On May 28, 1:16*pm, "tshad" <t...@dslextreme.comwrote:
I am calling a function that requires an Exception which you normally get
from a Catch ( Catch(Exception exc)).

But I have a message I am trying to pass to this funtion, but since it
requires an Exception object, I can't just send the message.

First of all Exception is just another class in the framework, you can
create instances of it as you please.
most probably you are calling a logging method.
You can simply say MyFunction ( new Exception("eereer"));

I advise you to do so though.
Exception has other properties (StackTrace, etc) that are usually used
to find more info about an error. You have to try your method in case
it expect those fields populated.
Also you could create an override of that method that simply receive a
String.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Richard Skopal | last post: by
5 posts views Thread by Michael | last post: by
11 posts views Thread by Mark B | last post: by
4 posts views Thread by illegal.prime | last post: by
reply views Thread by rosydwin | last post: by

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.