You're close but missing a few things. I'd highly recommend you read the following, there's some good information there.
http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx
In your case, there's a few key things wrong with your code.
1) You've got some text in your exception that you're calling the message, but you're not actually doing anything with it. I'm guessing you want this to be the exception message text when no other text is specified? In this case, you can just pass that message along to the appropriate base constructor...
- public NegativeSalaryException()
-
: base (msg)
-
{
-
}
You may also want to consider exposing the constructor that takes a string as a parameter, so users of the exception can specify their own message.
- public NegativeSalaryException(string message)
-
: base (message)
-
{
-
}
2) When you throw your exception, you aren't providing a message. This kind of ties in with the above... but the general method of throwing an exception is to provide some text with it. For example, if I were throwing a generic exception I'd do something like this...
- public static float Divide(float numerator, float denominator)
-
{
-
if (denominator == 0)
-
throw new Exception("Cannot divide by zero!");
-
-
return numerator / denominator;
-
}
We provide this so the user gets some feedback as to what went wrong. To correct that, either do as mentioned in the above, or provide an exception message for your exception at the time it is thrown.
3) Last one, and this is more of a suggestion than a requirement. When you catch an exception, I personally find it helpful to provide an exception parameter so you can access information about the exception. With what you have, you're just catching the exception type, but the actual exception is lost. You can access this simply by providing a variable name for the exception...
- try
-
{
-
// some code
-
}
-
catch (Exception e)
-
{
-
Console.WriteLine("An exception occurred!");
-
Console.WriteLine(e.Message);
-
Console.WriteLine(e.StackTrace);
-
}
Hopefully that helps. As always, I'd recommend reading the MSDN doc on this or checking out a few tutorials on exception handling. They provide much more comprehensive information than I can :) Good luck!