A method is supposed to return an int. How should it handle exceptions?

I'm using Visual Studio 2017 with C#.
I have a method like this.  It's supposed to convert a string to an integer.  If the string does not meet certain conditions there is an exception.
What's the best way to let whatever called this method know the details of the exception?

        public int GetScore(string s)
        {
            try
            {
                int score = 0;
                int totalScore = 0;
                string individualCard = "";
                bool oneJokerFound = false;
                bool twoJokersFound = false;
                int number;
                if (s == null)
                {
                    return 0;
                }
                else if (s == "")
                {
                    return 0;
                }
                for (int i = 2; i < s.Length; i += 3)
                {
                    if (s.Substring(i, 1) != ",")
                    {
                        throw new Exception("Incorrectly formatted string");
                    }
                }
                s = s.Replace(",", "");
                do
                {

}

Open in new window

AlHal2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Don't wrap the method body in a try / catch. Wrap the call in a try catch:

And don't throw an Exception. If you're going to throw, use a more suitable exception type such as FormatException:

throw new FormatException("Error message goes here");

try {

    var myInt = GetScore(someVar);

} catch (FormatExceptione e) {

    Debug.WriteLine(e.Message);

}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlHal2Author Commented:
The call is also a method.
Are there any guidelines for which methods should have a try and catch and which should not?
Chris StanyonWebDevCommented:
If you don't intend to handle the exception inside the method, then don't catch it. In your code, you're not actually doing anything with the exception, so just throw it. Only have a catch block if you intend to do something about it, otherwise just let it bubble up.

If the call is also a method and you don't intend to deal with it there either, then don't catch it. You only want to handle the exception when you can actually do something about it.

Exceptions will keep bubbling up until they're eventually caught. You may have an unhandled Exception handler, in which case that will catch it but your program will end. If you don't have any Exception handlers up the stack, then your program will crash with an Unhandled Exception errror.
AlHal2Author Commented:
Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.