Create Exception object

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

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
 
tshad said:
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.
 
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
 
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.
 
Back
Top