What's the best way to raise an exception in C#?
try
{
throw new DivideByZeroException();
}
catch (DivideByZeroException ex)
{
LogHelper.Error("TEST EXCEPTION", ex);
}
Short answer:
throw new Exception("Test Exception");
You will need
using System;
Build a custom exception for testing purposes ? Then you could add whatever custom properties you want the exception to carry with it on it's way through the exception handling / logging process...
[Serializable]
public class TestException: ApplicationException
{
public TestException(string Message,
Exception innerException): base(Message,innerException) {}
public TestException(string Message) : base(Message) {}
public TestException() {}
#region Serializeable Code
public TestException(SerializationInfo info,
StreamingContext context): base(info, context) { }
#endregion Serializeable Code
}
in your class
try
{
throw new TestException();
}
catch( TestException eX)
{
LogHelper.Error("TEST EXCEPTION", eX);
}
throw exceptionhere;
Isn't it?
Example I found was
if (args.Length == 0)
{
throw new ArgumentException("A start-up parameter is required.");
}