This will be the last time i ask a question about this :).
I have for a couple off hours now tried to get my head around custom exception handling, how and when to use it. I have asked here two times before about this, and i am really sorry for asking so many times but you people here always so good to explain and help!
I think i now know... just want your thougts about it.
I know there is better ways of solving this but the main purpose of this for me to understand the custom exception and throw statment.
This i my custom exception class:
[Serializable]
class CustomException : Exception
{
/// <summary>
/// Just create the exception
/// </summary>
public CustomException()
: base() {
}
/// <summary>
/// Create the exception with description
/// </summary>
/// <param name="message">Exception description</param>
public CustomException(String message)
: base(message) {
}
/// <summary>
/// Create the exception with description and inner cause
/// </summary>
/// <param name="message">Exception description</param>
/// <param name="innerException">Exception inner cause</param>
public CustomException(String message, Exception ex)
: base(message, ex) {
}
}
This is may class where i use it:
public static int ParseParse(string inInt)
{
int input;
if (!int.TryParse(inInt, out input))
{
MessageBox.Show("Only use numbes!");
return -1;
}
else if (input <= 0)
{
try
{
throw new CustomException("The price must be greater than zero");
}
catch (CustomException ex)
{
MessageBox.Show(ex.Message, "No negativ numbers or zero");
return -1;
}
}
else
{
return input;
}
}
And this is where i call this class above:
if (HelpClass.ParseInput(txtPrice.Text) <= 0)
{
ok = false;
}
else
{
newGame.Price = HelpClass.ParseInput(txtPrice.Text);
_mGames.AddNewGame(newGame);
}