throw ex

C

Cheryl

If I throw ex it does not give me enought informtion, if I throw
strmessage would this actually throw the message that I've created
plus the ex.message string?


catch(Exception ex)
{
strmessage = "Error Logger.Instance.LogError: " +
ex.message;
throw strmessage;
}
 
N

Nicholas Paldino [.NET/C# MVP]

Cheryl,

While it is possible to throw an instance of any type in the CLR, in C#,
you are limited to throwing objects that derive from Exception. If you need
more information, you can set the Message property of the Exception class.

However, you should try and find an exception that is already defined
that matches your condition, and use that. If not, then you should derive
from the Exception (or ApplicationException) class, so that types of your
exception can be caught (this would be the indicator of the particular
condition that you have, not the Message).

Hope this helps.
 
J

Jay B. Harlow [MVP - Outlook]

Cheryl,
In addition to the other comments, I would consider using either:

throw new Exception("Error Logger.Instance.LogError: ", ex);

Or

throw new ApplicationException("Error Logger.Instance.LogError: ", ex);

Or

throw new LoggerException("Error Logger.Instance.LogError: ", ex);

Where LoggerException is a custom exception object, that you can catch
specifically or pass other information along with.

The ex parameter in the above is the Inner Exception, it allows you to
"combine" the existing exception with a new exception in a Catch block.

The following article discusses how to define an exception so it plays
nicely with the Framework (including remoting).

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp08162001.asp

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top