not calling the base constructor in user defined Exception

A

Avi

Hi all,

I'm trying to write an exception with overloaded constructor, where one will
raise an exception while the other will only write to log. However, trying
the follwoing did not work
throw new NMSTestException("My Message", 0);

Please let me know what am I doing wrong

public class NMSTestException : Exception
{
public NMSTestException(string Message):base(Message)
{
// raise
}
public NMSTestException(string Message, int dummy)
{
// do not raise, write to log only
}

}
 
J

Jon Skeet [C# MVP]

Avi said:
I'm trying to write an exception with overloaded constructor, where one will
raise an exception while the other will only write to log. However, trying
the follwoing did not work
throw new NMSTestException("My Message", 0);

Please let me know what am I doing wrong

public class NMSTestException : Exception
{
public NMSTestException(string Message):base(Message)
{
// raise
}
public NMSTestException(string Message, int dummy)
{
// do not raise, write to log only
}

}

An exception is not thrown by creating it - it's thrown by throwing it.
If you want to create an instance of an exception without throwing it,
just don't use "throw":

NMSTestException foo = new NMSTestException("hello");
 

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