-2

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);
                }
Newbie1337
  • 179
  • 1
  • 11
  • Why don't you return simply `-1` instead of throwing and catching an exception. – Eser Sep 12 '15 at 21:02
  • There is no point to catch the exception that must always be thrown. (The entire catch in 'ParseParse' and corresponding and MessageBox.show should probably be removed from the method; make sure to post *accurate* code.) – user2864740 Sep 12 '15 at 21:03
  • I think duplicate [don't use exceptions for flow control](http://stackoverflow.com/questions/729379/why-not-use-exceptions-as-regular-flow-of-control) should cover your case. Side note: Have you noticed insane amount (-4) of negative votes on answer you've accepted? – Alexei Levenkov Sep 12 '15 at 21:25
  • I did not see that Alexei.. must have accepted wrong awnser.. i have changed that now.. – Newbie1337 Sep 12 '15 at 21:35

1 Answers1

2

If you really want to throw an exception here, then catching it in the same method makes no sense - just get rid of exception handling all together. You should throw the exception in ParseParse and catch it somewhere else in the call hierarchy. Remember that exceptions are propagated up the call stack, so you can even catch an exception in the Main method if it hasn't been already handled, no matter where the exception was thrown.

else if (input <= 0)
{
    throw new CustomException("The price must be greater than zero");
}

Now you can use the method like this:

try
{
    if (HelpClass.ParseInput(txtPrice.Text) <= 0) // No more code in the try block will be executed if ParseParse throws an exception
    {
        ok = false;
    }
    else
    {
        newGame.Price = HelpClass.ParseInput(txtPrice.Text);
        _mGames.AddNewGame(newGame);
    }
}
catch (CustomException ex)
{
    // exception handling logic goes here
}
Kapol
  • 6,025
  • 3
  • 18
  • 42