Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

Create Exception object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 28 May 2008 18:16 GMT
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
Chris Shepherd - 28 May 2008 18:20 GMT
> 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.
[quoted text clipped - 3 lines]
>
> 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.
Peter Bromberg [C# MVP] - 28 May 2008 19:05 GMT
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)).
[quoted text clipped - 10 lines]
>
> Tom
Ignacio Machin ( .NET/ C# MVP ) - 28 May 2008 21:59 GMT
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.